Module 3
Functions
Functions
• A function is a series of instructions or group of statements with
one specific purpose.
• A function is a program segment that carries out some specific,
well defined task.
• A function is a self contained block of code that performs a
particular task.
Types of functions
C functions can be classified into two types
1. Library functions /pre defined functions /standard functions
/built in functions
2. User defined functions
Library Functions
• These functions are defined in the library of C compiler which
are used frequently in the C program.
• These functions are written by designers of c compiler.
• C supports many built in functions like
• Mathematical functions
• String manipulation functions
• Input and output functions
• Memory management functions
• Error handling functions
EXAMPLE:
pow(x,y)-computes xy
sqrt(x)-computes square root of x
printf()- used to print the data on the screen
scanf()-used to read the data from keyboard.
User defined functions
• The functions written by the programmer /user to do the specific
tasks are called user defined function(UDF’s).
• The user can construct their own functions to perform some
specific task.
• This type of functions created by the user is termed as User
defined functions.
Elements of User Defined
• Function The Three Elements of User Defined function structure
consists of :
1. Function Definition
2. Function Declaration
3. Function call
Function Definition
• A program Module written to achieve a specific task is called as
function definition.
• Each function definition consists of two parts:
i. Function header
ii. Function body
i. Function header
Syntax: datatype function name(parameters)
• It consists of three parts:
a) Datatype: The data type can be int, float,char ,double,void.
This is the data type of the value that the
function is expected to return to calling
function.
b) function name: The name of the function should be a valid
identifier.
c) parameters
• The parameters are list of variables enclosed within
parenthesis.
• The list of variables should be separated by comma.
Ex: void add( int a, int b)
In the above example the return type of the function is void ,
the name of the function is add and .The parameters are 'a' and
'b' of type integer.
ii. Function body
The function body consists of the set of instructions enclosed between { and } .
The function body consists of following three elements:
a) declaration part: variables used in function body.
b) executable part: set of Statements or instructions to do specific activity.
c) return : It is a keyword,it is used to return control back to calling function.
If a function is not returning value then statement is:
return;
If a function is returning value then statement is:
return value;
2. Function Declaration
• The process of declaring the function before they are used is
called as function declaration or function prototype.
• function declaration Consists of the data type of function, name
of the function and parameter list ending with semicolon.
3. Function Call
• The method of calling a function to achieve a specific task is
called as function call.
• A function call is defined as function name followed by
semicolon.
• A function call is nothing but invoking a function at the required
place in the program to achieve a specific task.
Ex: void main()
{
add( ); // function call without parameter
}
• Formal Parameters and Actual Parameters
• Formal Parameters:
• The variables defined in the function header of function definition are
called formal parameters.
• All the variables should be separately declared and each declaration
must be separated by commas.
• The formal parameters receive the data from actual parameters.
Actual Parameters:
• The variables that are used when a function is invoked)in
function call) are called actual parameters.
• Using actual parameters, the data can be transferred from
calling function. to the called function.
• The corresponding formal parameters in the function definition
receive them.
• The actual parameters and formal parameters must match in
number and type of data.