Let’s see how we can write a function which takes no argument but returns a value. Type 2 – Functions that take NO argument and returns A value I would write a function which would 1. Ask the user to input a number 2. Calculate its square 3. Return the calculated value to main function 4. Main function will display calculated result #include <stdio.h> #include <conio.h> void square() { int number; printf(“\nPlease enter a number :”); Output There would be no output as it has a syntax error. The error is LVALUE REQUIRED scanf(“%d”, &number); number = number * number; return number; } void main() { clrscr(); ...
if-else-if Consider a scenario where we have to check multiple conditions which are related to each other. For example we enter marks of a student and then we assign a grade to it. Marks Grade >=80 A >=70 and <80 B >=60 and <70 C >=50 and <60 D <50 Fail In above scenario, if we use simple if then we need to use FIVE if statements. These if’s would be checked every time, even if the first condition is true. Syntax if(condition) { // Statements if true } else if (condition) { //Statements if true } else if (condition) { //Statements if true } else if (condition) { //Statements if true } else if (condition) { //Statements if true } . . else { } With à if Example 19 - Grading system using if With à if-else-if Exam...
Comments
Post a Comment