Modularity:
break up the problem into sub-
problems.
Top-down design approach: when attempting to
solve a sub-problem at one level, new subproblems 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
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
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 = 3.14 * radius * radius;
return (area);
}
Dr. Soha S. Zaghloul
4
The
name of a function obeys the same rules as
those of an identifier.
Therefore, the following function names are NOT
accepted:
Function name
Reason
Circle-Area
Hyphen is not allowed
1Area
Should start with a letter
continue
Reserved word
In
the previous example, Circle is the function
name.
Dr. Soha S. Zaghloul
5
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
double Circle (double radius)
{
double PI = 3.14;
double area;
area = 3.14 * radius * 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
7
void PrintLetter (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
8
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
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
double Circle (double radius)
{
double PI = 3.14;
double area;
area = 3.14 * radius * 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
11
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
12
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
13
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
int Sum (int num1, int num2, int num3)
{
int total;
total = num1 + num2 + num3;
return (total);
}
The
above example could be written as:
int Sum (int num1, int num2, int num3)
{
return (num1 + num2 + num3);
}
The
above function has only one return
statement. There is no declaration, initialization,
nor processing.
Dr. Soha S. Zaghloul
15
1.
2.
3.
4.
5.
6.
7.
8.
27.
28.
29.
30.
31.
#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
1. #include <stdio.h>
2. // Function prototype
3. double CircleArea(double radius);
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
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
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
1. #include <stdio.h>
2. // Function prototype
3. double CircleArea(double radius);
// FUNCTION PROTOTYPE
4. int main (void)
5. {
6.
double circle, r;
7.
printf (“Enter circle radius> “);
8.
scanf (“%f”, r);
9.
circle = CircleArea( r );
10. printf (“Area of circle = %f”, circle);
11. return (0);
12. } // end main
13.
14.
15.
16.
17.
18.
19.
20.
// FUNCTION CALL
// start define your functions
double CircleArea (double radius)
{
double area;
area = 3.14 * radius * radius;
return (area);
} // end CircleArea
// end of program
Dr. Soha S. Zaghloul
// FUNCTION HEADER AND DEFINITION
// RETURN VALUE
19
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
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
1. #include <stdio.h>
2. // Function prototype
3. int power(int num1, int num2);
// FUNCTION PROTOTYPE
4.
5.
6.
7.
8.
9.
10.
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
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
// 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
1. #include <stdio.h>
2. // Function prototype
3. char DisplayMenu (void);
// FUNCTION PROTOTYPE
4. int main (void)
5. {
6.
char option;
7.
option = DisplayMenu() ;
8.
return (0);
9. } // end main
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
// FUNCTION CALL
// start define your functions
char DisplayMenu (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 DisplayMenu
// end of program
Dr. Soha S. Zaghloul
// FUNCTION HEADER
// returned value
23
1. #include <stdio.h>
2. // Function prototype
3. char DisplayMenu (void);
// FUNCTION PROTOTYPE
4. int main (void)
5. {
6.
char option;
7.
option = DisplayMenu() ;
8.
return (0);
9. } // end main
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
// FUNCTION CALL
// start define your functions
char DisplayMenu (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 DisplayMenu
// end of program
Dr. Soha S. Zaghloul
// FUNCTION HEADER
// returned value
24
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
© Copyright 2026 Paperzz