The program may become too large and complex and as a result the task of debugging, testing and maintaining becomes difficult. if a program is divided into functional parts , then each part may be independently coded and later combined into single unit .These sub programs are known as functions
Functions are the user defined data types.
Functions are having modular approach.
It facilitates top - down modular programming.
A function may be used by many other programs.
It is easy to locate and isolate faulty function for further investigations.
Functions are of two types :
User defined functions
User defined functions are the functions which are defined by the user itself .
Main Program Function2 Function3 Function 1
return type <function name > (arguments)
local variable declarations ;
execute statement 1;
execute statment2 ;
A function must follow the same rules of information as other variables names .
Additional care must be taken to avoid duplicating library routine names or operating system commands .
The arguments may be void.
The argument list contains valid variable names separated by commas.
The list must be surrounded by parenthesis .
No semicolon follows the parenthesis.
The argument receive value form the calling function ,thus providing a means for data communication from the calling function to the called function.
All the arguments should be declare with its data type.
Categories of function
Category 1: Function with no arguments no return value
Category 2: Functions with arguments and return values
Category 3: Functions with arguments and return values
Class student // declaration of class
void getdata() // Function declaration
cout<< “Input the name of the student” ;
cout<< “Input the rollno” ;
cout<< “Input the marks of the student” ;
void putdata() // declaration of another function
cout<< “Name = “ <<name<<endl;
cout<< “Roll no = ” <<rollno<<endl;
cout<< “Marks = ” <<marks<<endl;
class student obj;
obj.getdata(); // calling of the function
obj.putdata(); // calling of the another function
Input the name of student XYZ
Input the rollno 1
Input the marks of the student 49.02
void sum(int a ,int b)
cout<< “The addition is “ <<c;
cout<< “We are in main Function ”;
We are in main Function
The addition is 30
The main objective of passing arguments to function is message passing. The message passing is also known as communication between two functions i.e., between caller and callee functions. There are three methods which can pass values to the function:-
Call by value (Pass by value)
Call by address (Pass by address)
Call by reference (pass by reference)
The value of the actual argument is passed to the formal arguments and operation is done on the formal arguments.
Any change in the formal arguments does not effect the actual arguments because formal arguments are photo copy of the actual arguments.
When the function is called it does not affect the actual arguments.
Changes are made in the f0rmal arguments are local to the block of called function
void change (int,int);
void change (int a , int b)
Instead of passing values address is passed.
Function operates on address rather than values.
Formal arguments are pointers to the actual arguments.
Changes made in the argument are permanent.
void change (int * , int *);
change( &x , &y);
void change (int *a , int *b)
*temp = *a;
*a = *b;
*b = *temp;
When a function is declared as inline ,the compiler copies the code of the function in the calling function.
Function body is inserted in place of function call during compilation.
Passing of control between caller and callee function is avoided.
The inline function is mostly used when calling of function is small. it is advisable to use the inline function for small programs.
Inline mechanism increases execution performance in terms of speed.the program using inline function needs more memory space since the inline functions are copied at every point