Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Extra Examples.

Similar presentations


Presentation on theme: "Functions Extra Examples."— Presentation transcript:

1 functions Extra Examples

2 6. Function type – example (1)
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. double Circle (double radius) { double PI = 3.14; double area; area = * radius * radius; return (area); } Dr. Soha S. Zaghloul 2

3 7. Function type – example (2)
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. void PrintLetter (char letter) { int i; for (i= 1; i<= 10; i++) printf (“/n”, letter); } Dr. Soha S. Zaghloul 3

4 9. Function arguments – example (1)
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. double Circle (double radius) { double PI = 3.14; double area; area = * radius * radius; return (area); } Dr. Soha S. Zaghloul 4

5 10. Function arguments – example (2)
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. double Average (double sum, int count) { return (sum/count); } Dr. Soha S. Zaghloul 5

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

7 13. Function statements – example (1)
The above example could be written as: The above function has only one return statement. There is no declaration, initialization, nor processing. int Sum (int num1, int num2, int num3) { int total; total = num1 + num2 + num3; return (total); } int Sum (int num1, int num2, int num3) { return (num1 + num2 + num3); } Dr. Soha S. Zaghloul 7

8 Functions are defined after the end of the main function
14. Functions defintion #include <stdio.h> int main (void) { ------ } // end main // start define all functions double CircleArea (double radius) } // end CircleArea // end of program Functions are defined after the end of the main function Dr. Soha S. Zaghloul 8

9 15. Functions prototypes #include <stdio.h> // Function prototype double CircleArea(double radius); int main (void) { ------ } // end main // start define all functions double CircleArea (double radius) } // end CircleArea // end of program In addition, a prototype of the function should be written before the main function. Dr. Soha S. Zaghloul 9

10 16. Function call – EXAMPLE (1)
#include <stdio.h> // Function prototype double CircleArea(double radius); // FUNCTION PROTOTYPE int main (void) { double circle, r; printf (“Enter circle radius> “); scanf (“%f”, r); circle = CircleArea( r ); // FUNCTION CALL printf (“Area of circle = %f”, circle); } // end main // start define your functions double CircleArea (double radius) // FUNCTION HEADER AND DEFINITION double area; area = 3.14 * radius * radius; return (area); // RETURN VALUE } // end CircleArea // end of program Dr. Soha S. Zaghloul 10

11 19. multi-arguments Functions – EXAMPLE (1)
#include <stdio.h> // Function prototype int power(int num1, int num2); // FUNCTION PROTOTYPE int main (void) { int result; result = power (2, 5) ; // actual parameters  num1 = 2, num2 = 5 (ie 25 = 32) result = power (5, 2); // actual parameters  num1 = 5, num2 = 2 (ie 52 = 25) } // end main // start define your functions int power( int num1, int num2) // formal parameters int i; int product = 1; for (i= 1; i< num2; i++) product *= num1; return product; } // end power // end of program Dr. Soha S. Zaghloul 11

12 20. zer0-argument Functions – EXAMPLE (1)
#include <stdio.h> // Function prototype char DisplayMenu (void); // FUNCTION PROTOTYPE int main (void) { char option; option = DisplayMenu() ; // FUNCTION CALL } // end main // start define your functions char DisplayMenu (void) // FUNCTION HEADER 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); // returned value } // end DisplayMenu // end of program Dr. Soha S. Zaghloul 12

13 21. zero-argument Functions – EXAMPLE (1)
#include <stdio.h> // Function prototype char DisplayMenu (void); // FUNCTION PROTOTYPE int main (void) { char option; option = DisplayMenu() ; // FUNCTION CALL } // end main // start define your functions char DisplayMenu (void) // FUNCTION HEADER 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); // returned value } // end DisplayMenu // end of program Dr. Soha S. Zaghloul 13

14 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


Download ppt "Functions Extra Examples."

Similar presentations


Ads by Google