function. A „C‟ program is made of one or more functions, and one and only one of which must
be main( ) function. The execution of the program always starts with main( )
Advantages of functions:
1. It facilitates top-down modular programming approach as shown in the figure. In top
down modular approach, a program is divided into a main module and its related
modules. Each module can again be divided into sub modules.
Main program
Function 1 Function 2 Function3
2. The length of the source program can be reduced by using functions at appropriate
places.
3. It is easy to locate and isolate a faulty function during debugging.
4. A function may be used by many other programs.
Types of functions:
In „C‟ language, functions are classified into two types. They are
a) Library functions (or) Built-in functions
b) User defined functions
a) Library Functions: Library functions are predefined functions and supplied along with the
compiler and these can be used in any „C‟ program. They are also known as Built-in functions
and all these functions are available in „C‟ library. Ex: scanf( ), printf( ), gets( ) etc.
b) User defined Functions: The functions that are defined by the users are called as user-
defined functions. Ex: main( ) is an example of user defined function.
Elements of User defined Functions (or) function components: Every function has the
following elements associated with it.
i. Function definition (or) Function implementation
ii. Function call
iii. Function declaration (or) Function prototype
i. Function definition: The function definition is an independent program module that
, iswritten to implement the specific task.
A function definition has two parts namely
1. Function body
2. Function header
The general form of a function definition to implement two parts is given below.
return_type function_name (parameter list)
{
Local variable
declaration;Executable
statement1; Executable
statement2;
-------------------------
-------------------------
return statement;
}
* Function header: The function header consists of three parts
a) return_type
b) function_name
c) parameter(formal) list
return type: A function may or may not send back any value to the calling function. A function
type specifies the data type of value that the function is expected to return to the program calling
the function. If a function is not returning anything, then we need to specify the function type as
void.
Function-name: The function name is any valid C identifier and the name should be appropriate
to the task performed by the function.
Parameter list: The parameter list contains declaration of variables separated by commas and
surrounded by parenthesis. The parameter list declares the variables that will receive the data
sent by the calling program. They serve as input data to the function to carry out the specified
task.
* Function body: The function body contains the declarations and statements necessary for
performing the required task. The body enclosed in braces, contains three parts in the order
givenbelow.
a) Local declarations that specify the variables needed by the function
b) Function statements that perform the task of the function
, c) A return statement that returns the value evaluated by the function.
If a function does not return any value, we can omit the return statement. But the
functiontype should be specified as void.
Ex: int sum(int a, int b) /*function header*/
{
int rst;
rst=a+b; /*function
body*/return(rst);
}
ii. Function call: A function can be called by writing the function name followed by a
listof actual parameters, if any, enclosed in the parenthesis and terminated by a semicolon.
Ex: main( )
{
int z,x,y;
scanf(“%d%d”,&x,&y)
;
z=maximum(x,y); /*function call*/
printf(“z=%d”,z);
}
In the above program, the statement z=maximum(x,y); invokes the function maximum( )
with two integer parameters. Executing the function call statement causes the control to be
transferred to the first statement in the function body. When a return statement is executed in
the function body, the control is transferred back to the calling function. In the absence of a
return statement, the closing brace acts as a void return.
iii. Function declaration: All functions in a „C‟ program must be declared, before they are
invoked. A function declaration consists of four parts.
a) return_type
b) function_name
c) parameter list
d) terminating semicolon
The general form of declaring the function is return_type function_name(parameter list);
Ex: int maximum(int m, int n);
Return statement: A function may or may not send back any value to the calling function. It
can be done through the return statement. When a return statement is executed, the control is
transferred to the calling function.