Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions.

Similar presentations


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

1 functions

2 Modularity: break up the problem into sub- problems.
1. Why functions? Modularity: break up the problem into sub- problems. Top-down design approach: when attempting to solve a sub-problem at one level, new sub- problems arise at a lower level. Code sharing: In big projects, the coding is assigned to different people. The code is then assembled in a single code. This is impossible to achieve without the concept of functions. Create libraries: in the same way there are stdio.h and string.h libraries, you may create your own library. Dr. Soha S. Zaghloul 2

3 2. Functions manipulation
In order to deal with functions, we have to know the following: Definition: writing the statements of the function Prototypes: functions declaration Calling: how to call a function Dr. Soha S. Zaghloul 3

4 A function may have arguments. A function has one or more statements.
3. Functions definition Any function has a name. Any function has a type. A function may have arguments. A function has one or more statements. The following function calculates the area of a circle: double Circle (double radius) { double PI = 3.14; double area; area = * radius * radius; return (area); } Dr. Soha S. Zaghloul 4

5 The name of a function obeys the same rules as those of an identifier.
4. Function name The name of a function obeys the same rules as those of an identifier. Therefore, the following function names are NOT accepted: In the previous example, Circle is the function name. Function name Reason Circle-Area Hyphen is not allowed 1Area Should start with a letter continue Reserved word Dr. Soha S. Zaghloul 5

6 A function returns a single value at most to the main program.
5. Function type A function returns a single value at most to the main program. The type of the function is the same as that of the returned type. If the function does not return any value, then the type is “void”. Dr. Soha S. Zaghloul 6

7 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 7

8 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 8

9 There is no restriction on the number of arguments.
8. Function arguments The function arguments are the variables through which values are transferred to the function from the main program (or from another function) to work with. There is no restriction on the number of arguments. We may have no arguments. Each argument has a name and is preceded by its type. The order of arguments listing is relevant. This will be more illustrated when we discuss function calling. Dr. Soha S. Zaghloul 9

10 8. Function arguments (Cont’d)
There is NO relation between the type of function and the number and/or type of arguments. For example: A void function may have arguments; A double function may have no arguments; An int function may have an argument of type char; and so on… The arguments are also sometimes called the parameter list of a function. Dr. Soha S. Zaghloul 10

11 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 11

12 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 12

13 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 13

14 Therefore, the structure is as follows:
12. Function statements The function structure is similar to the main program, which is actually a function. Therefore, the structure is as follows: Declaration part if needed. Refer to slide 13. Initialization part if needed. Processing part if needed. Return statement for non-void functions Dr. Soha S. Zaghloul 14

15 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 15

16 Functions are defined after the end of the main function
14. Functions defintion #include <stdio.h> int main (void) { ------ return (0); } // 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 16

17 15. Functions prototypes #include <stdio.h> // Function prototype double CircleArea(double radius); int main (void) { ------ return (0); } // 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 17

18 15. Functions prototypes (cont’d)
Note that the function prototype ends with a semicolon, while the function header does not. The program execution starts at the main function; then each function is executed when called. Dr. Soha S. Zaghloul 18

19 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); return (0); } // 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 19

20 In addition, r should have a value before calling the function.
17. Function call In line 9 of the previous example, we call the function CircleArea:- The value returned by a function (area) is stored in a variable declared in the main function (circle). The type of the declared variable should be the same as that of the function (double). The variable used in the actual argument list (r) replaces the formal parameter (radius). The actual argument should also be declared in the main function. It should be the same type as the formal parameter. In addition, r should have a value before calling the function. Note that the types of the arguments are not written when calling the function in the main program. Be sure that the number and types of arguments in the prototype, the function call, and the function header are the same. Dr. Soha S. Zaghloul 20

21 18. multi-argument FUNCTIONs
When using multiple-argument functions, you must be careful: To include the correct number of arguments in the function prototype, function call, and function header. Also, the order of the actual arguments used in the function call must correspond to the order of the formal parameters listed in the function prototype or heading. Dr. Soha S. Zaghloul 21

22 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) return (0); } // 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 22

23 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 return (0); } // 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 23

24 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 return (0); } // 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 24

25 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 25


Download ppt "Functions."

Similar presentations


Ads by Google