CP II Unit IV Part II
CP II Unit IV Part II
Balagurusamy
- The prototype describes the function interface to the compiler by giving details such
as
o Number of arguments
o Type of arguments
o Type of return value
- Function prototype is a declaration statement in the calling program and is of the
following form:
- Syntax
Return_type fuction_name( argument-list);
o The argument-list contains the types and names of arguments that must be
passed to the function.
- Example
float volume (int x, float y, float z);
Function Definition
- void add( ) {
int a,b,sum;
cout<<”Enter two integers”<<endl;
cin>>a>>b;
sum=a+b;
cout<<”\nThe sum of two numbers is “<<sum<<endl;
}
- The above function add ( ) can also be coded with the help of arguments of
parameters as shown below:
Call by Reference:
- We have seen call by reference with traditional C approach in Unit IV Part-I notes,
but In C++ we can also use reference variable.
- Provision of the reference variables in C++ permits us to pass parameters to the
functions by reference.
- When we pass arguments by reference, the formal arguments in the called function
become aliases to the actual arguments in the calling function.
- This means that when the function is working with its own arguments, it is actually
working on the original data.
- Consider Following Function:
void swap(int &a, int &b){ // a and b are reference variable…
a=a+b;
b=a-b;
a=a-b;
}
- Now, if m and n are two integer variables, then the function call
swap(m,n);
- Will exchange the values of m and n using their reference variables a & b.
- Complete Program is as follows:-
#include<iostream.h>
void swap(int &a, int &b){
a=a+b;
b=a-b;
a=a-b;
}
main( ){
int m,n;
cout<< “ Enter m & n\n”;
cin>>m>>n;
swap(m,n);
cout<< “After swapping m=”<<m<< “n=”<<n;
}
Return by Reference:
- A function can also return a reference.
- Consider following function:
InLine Function:
- One of the objective of using functions in a program is to save some memory space,
which becomes a appreciable when a function is likely to be called many times.
- However, every time a function is called, It takes lots of extra time in executing series
of instructions for task such as
o Jumping to function,
o Saving registers,
o Pushing arguments in to the stack,
o Returning to the calling function.
- When function is small, a substantial percentage of execution time may be spent in
such overheads.
- C++ has solution to this problem. To eliminate the cost of calls to small functions, C+
+ proposes a new feature called inline function.
- An inline function is a function that is expanded in line when it is invoked.
- That is, the compiler replaces the function call with the corresponding function code.
- Syntax of inline function is:
inline Function_Header{
//function body…
}
- Example:-
inline int add( ) {
int a,b,sum;
cout<<”Enter two integers”<<endl;
cin>>a>>b;
sum=a+b;
return sum;
}
main( ){
cout<<”\nThe sum of two numbers is “<<add( )<<endl;
cout<<”\nThe sum of two numbers is “<<add( )<<endl;
cout<<”\nThe sum of two numbers is “<<add( )<<endl;
}
- Compiler may ignore inline function if function definition is too long ot too
complicated & compile the function as a normal function.
- Inline function makes a program run faster but takes extra memory.
- Some Situations where inline function may not work are:
o For function returning values, if a loop, a switch or goto exists.
o For a functions not returning values, if a return statement exits.
o If function contains static variables.
o If inline functions are recursive.
Default Argument
Output:-
DDDDDDDDDD
DDDDDDDDDDDDDDD
MMMMMMMMMMMMMMMMMMMMMMMMM
Constant Arguments:
Note:-
Kindly refer your note book for function overloading, friend function & virtual function. We
will discuss these concepts again in details with classes & objects…