Extra Examples 6 FUNCTION TYPE EXAMPLE 1 double

Extra Examples

6. FUNCTION TYPE – EXAMPLE (1) double Circle (double radius) { double PI = 3. 14; double area; area = 3. 14 * radius; return (area); } � In the above example, the function returns the area of the circle. � Since area is defined in the function as double, then the function is of type double. Dr. Soha S. Zaghloul 2

7. FUNCTION TYPE – EXAMPLE (2) void Print. Letter (char letter) { int i; for (i= 1; i<= 10; i++) printf (“/n”, letter); } � The above function should print letter ten times on ten lines. � Since it does not return anything, then the type of the function is void. � No return statement should be associated with a function of type void. Dr. Soha S. Zaghloul 3

9. FUNCTION ARGUMENTS – EXAMPLE (1) double Circle (double radius) { double PI = 3. 14; double area; area = 3. 14 * radius; return (area); } � In the above example, the parameter list consists of one variable radius, which is of type double. � Note that radius is not declared within the function. Dr. Soha S. Zaghloul 4

10. FUNCTION ARGUMENTS – EXAMPLE (2) double Average (double sum, int count) { return (sum/count); } � In the above example, the parameter list consists of two variables sum (of type double), and count (of type int) � Arguments are separated by commas. � Note that the return statement may include an arithmetic operation. Dr. Soha S. Zaghloul 5

11. FUNCTION ARGUMENTS – EXAMPLE (3) void Menu(void) { printf (“ +: Addition n”); printf (“ -: Subtraction n”); printf (“ *: Multiplicaiton n”); printf (“ /: Division n”); printf (“ %: Modulus n”); } � The above function contains no arguments, and is of type void. � It is called to display the shown menu. Dr. Soha S. Zaghloul 6

13. FUNCTION STATEMENTS – EXAMPLE (1) int Sum (int num 1, int num 2, int num 3) { int total; total = num 1 + num 2 + num 3; return (total); } � The above example could be written as: int Sum (int num 1, int num 2, int num 3) { return (num 1 + num 2 + num 3); } � The above function has only one return statement. There is no declaration, initialization, nor processing. Dr. Soha S. Zaghloul 7

14. FUNCTIONS DEFINTION 1. 2. 3. 4. 5. 6. 7. 28. 29. 30. 31. #include <stdio. h> int main (void) { -----} // end main // start define all functions double Circle. Area (double radius) { -----} // end Circle. Area // end of program � Functions are defined after the end of the main function Dr. Soha S. Zaghloul 8

15. FUNCTIONS PROTOTYPES 1. #include <stdio. h> 2. // Function prototype 3. double Circle. Area(double radius); 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. int main (void) { -----} // end main // start define all functions double Circle. Area (double radius) { -----} // end Circle. Area // end of program � In addition, a prototype of the function should be written before the main function. Dr. Soha S. Zaghloul 9

16. FUNCTION CALL – EXAMPLE (1) 1. #include <stdio. h> 2. // Function prototype 3. double Circle. Area(double radius); // FUNCTION PROTOTYPE 4. int main (void) 5. { 6. double circle, r; 7. printf (“Enter circle radius> “); 8. scanf (“%f”, r); 9. circle = Circle. Area( r ); 10. printf (“Area of circle = %f”, circle); 11. } // end main 12. 13. 14. 15. 16. 17. 18. 19. // FUNCTION CALL // start define your functions double Circle. Area (double radius) { double area; area = 3. 14 * radius; return (area); } // end Circle. Area // end of program Dr. Soha S. Zaghloul // FUNCTION HEADER AND DEFINITION // RETURN VALUE 10

19. MULTI-ARGUMENTS FUNCTIONS – EXAMPLE (1) 1. #include <stdio. h> 2. // Function prototype 3. int power(int num 1, int num 2); // FUNCTION PROTOTYPE 4. int main (void) 5. { 6. int result; 7. result = power (2, 5) ; // actual parameters num 1 = 2, num 2 = 5 (ie 25 = 32) 8. result = power (5, 2); // actual parameters num 1 = 5, num 2 = 2 (ie 52 = 25) 9. } // end main 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. // start define your functions int power( int num 1, int num 2) // formal parameters { int i; int product = 1; for (i= 1; i< num 2; i++) product *= num 1; return product; } // end power // end of program Dr. Soha S. Zaghloul 11

20. ZER 0 -ARGUMENT FUNCTIONS – EXAMPLE (1) 1. #include <stdio. h> 2. // Function prototype 3. char Display. Menu (void); // FUNCTION PROTOTYPE 4. int main (void) 5. { 6. char option; 7. option = Display. Menu() ; 8. } // end main 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. // FUNCTION CALL // start define your functions char Display. Menu (void) { char choice; printf (“C: Area of a Circle n”); printf (“T: Area of a Triangle n”); printf (“S: Area of a Square n”); printf (“ Enter your choice> “); scanf (“%c”, choice); return (choice); } // end Display. Menu // end of program Dr. Soha S. Zaghloul // FUNCTION HEADER // returned value 12

21. ZERO-ARGUMENT FUNCTIONS – EXAMPLE (1) 1. #include <stdio. h> 2. // Function prototype 3. char Display. Menu (void); // FUNCTION PROTOTYPE 4. int main (void) 5. { 6. char option; 7. option = Display. Menu() ; 8. } // end main 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. // FUNCTION CALL // start define your functions char Display. Menu (void) { char choice; printf (“C: Area of a Circle n”); printf (“T: Area of a Triangle n”); printf (“S: Area of a Square n”); printf (“ Enter your choice> “); scanf (“%c”, choice); return (choice); } // end Display. Menu // end of program Dr. Soha S. Zaghloul // FUNCTION HEADER // returned value 13

22. SELF-CHECK EXERCISE Write a complete modular program that displays a menu to perform the four mathematical operations (+, -, *, /). Each operation should be then computed in a separate function. Dr. Soha S. Zaghloul 14
- Slides: 14