Académique Documents
Professionnel Documents
Culture Documents
F U N C TI O N S
A large and complex program can be divided into several functional parts. Each
part is called a sub-program. In C, such subprograms are referred to as functions.
Definition:
Types of functions:
i. Pre-Defined functions:
A function that was already defined in C software is known as a pre-defined
function.
i. Function Definition
i. Function Definition:
i. Function Header
ii.Function Body
Function Header:
A function header consists of 3 parts i.e., function type (return type), function name
and formal parameter list.
The function type specifies the type of value that the function is expected to return. The
function name is any valid C identifier. The parameter list declares the variables that
will receive the data sent by the calling program.
<returntype> function_name(parameter_list)
{
statement(s);
}
After defining a function , we must invoke the function to execute the function body. It
can be done by using the function name following by the list of actual arguments.
function_name(actual_args);
ex: sum(10,20);
Like variables, all functions in C program must be declared before they are
invoked. A Function declaration (prototype) consists of 4 parts.
returntype fun_name(parameter_list);
CATEGORIES OF FUNCTIONS:
function1() function2()
{ {
------ ------
------ ------
function2() }
-------
Ex: display()
printf(%d, x);
In this method, the calling function sends the data to the called function and also the
called function returns a value i.e., there is a two-way communication between the calling
and called functions.
function1() function2(y)
{ {
------- -------
------- return(e);
function2(x); }
-------
return(f);
}
iii. Arguments and No return values:
This approach seems to be wiser because the calling function can check for
the validity of data if necessary before it is handed over to the called function.
function1() function2(y)
{ {
------ -------
------ -------
function2(x); }
------
Ex: show(float i)
printf(%f, i);
There could be occasions when we define a function that does not take any
argument but returns a value to the calling function.
function1() function2()
{ {
-------- --------
-------- return(e);
function2(); }
--------
int a=5;
return(a);