C++M-I(14,15)
C++M-I(14,15)
To understand inline function, just recall the concept of macro and function.
A program using function
#include<iostream.h>
using namespace std;
int cube(int x)
{
return x*x*x;
}
main()
{
cout<<cube(3)<<endl; Output:
return 0;
} 27
Explanation:
CSE/C++/Module-I/Trilochan Rout/Lecture: 14, 15 2
In the above program, we have defined a function int cube(int x), that will take
a integer value and compute the cube of that number.
Always program execution starts from main().
Wherever there is any statement cube(3) inside main function, compiler will call
the function with argument 3. Then control of execution will jump to called
function and function will return cube of number 3 to main function. After
completion of called function,control will to main function.
So transfer of control from main function to called function and called function
to main function is an overhead to compiler.
Disadvantage:
Transfer of control from main function to called function and called function to
main function is an overhead to compiler.
Advantage:
Error checking will be done at compile time by the function.
Alternative solution:
Use of macro can reduce the overhead of compiler.
Explanation:
In the above program we have defined a macro which will calculate cube of a
number. Always program execution starts from main program. Whenever it will
find cube(x), complier will replace the cube(x) with code x*x*x, then it will
compute that expression.
Advantage:
Control of execution will not be transfer.
Disadvantage:
No error checking will be done at compile time.
Solution:
Make the function as inline function by using keyword inline.
Example:
inline int cobe(int x)
{
return x*x*x;
}
CSE/C++/Module-I/Trilochan Rout/Lecture: 14, 15 3
INLINE FUNCTION
#include<iostream>
using namespace std;
inline int cube(int x)
{
return x*x*x;
}
main() Output:
{
cout<<cube(3)<<endl; 27
return 0;
}
Explanation:
Now function int cube(int x) is a inline function, and whenever there is a
statement cube(3) it will copy the entire code of the function int cube(int x) to
the main function. Then that will be executed by compiler.
So unlike function call, there will not be any control transfer in between main
function and called function.
Advantage:
Inline function has both properties of a macro and a function. So here error
checking will also be done.
But macros are not functions.
Following are some situations where inline function may not work:
If a function is returning function, and function contain any control structure
statements like loop or goto statement or switch statement
If a function is returning nothing, and function contain return statement
If a function is recursive
If a function contains static data variable
The main() function cannot work as inline
If a function is very large
Note:
If a function is very large, in such a case inline function should not be used
because compiler copies the code of the function to the main function that
reduces the program execution speed.
CSE/C++/Module-I/Trilochan Rout/Lecture: 14, 15 4
Lecture: 15:
II. Function Overloading
C++ allows, to defining multiple functions with same name, is known as
Function Overloading or Function Polymorphism.
Function polymorphism means one function having many forms. The
overloaded functions must have
1- Different number of arguments
& 2- different types of arguments
In function overloading compiler determines which function to be called
depending upon parameter passed to during function call.
Example:
#include<iostream>
using namespace std;
return 0;
}
void add(int a,int b)
{
cout<<a+b<<endl;
return;
}
void add(float a,float b)
{
cout<<a+b<<endl;
return;
}
18
Explanation:
The first function call add(a,b)call the function having prototype void add(int,int
);. Because we are passing two parameter a & b of type int.
The function call add(x,y) call the function having prototype void
add(float,float);. Because we are passing two parameter x& y of type float.
The function call add(i,j) call the function having prototype void
add(double,double);. Because we are passing two parameter i& j of type
double.
The function call add(a,b,c) call the function having prototype void
add(int,int,int);. Because we are passing three parameter a, b & c of type int.
Principle of Function Overloading:
If two functions have similar number of arguments & similar type of
arguments,then that two functions cannot be overloaded.
Example:
void add(int , int );
int add(int , int );
These two functions cannot be overloaded. Compiler will report a error in this
case.
(Note: Return type will not be considered)
Example:
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
void add(float ,float);
void add(double ,double );
main()
{
add(2.6,6.7);
return 0;
}
void add(float a,float b)
{
cout<<"f1:"<<a+b<<endl; Output:
return;
}
f2:9.3
void add(double a,double b)
{
cout<<"f2:"<<a+b<<endl;
return;
}
Explanation:
You may be think that void add (float, float), but NO. Because we are passing
two constant values 2.6 & 6.7 to function add ().So complier willconsider
constant 2.6 & 6.7 as double type. So it will call 2 nd function which have
prototype void add (double, double);.
1. First compiler attempts to find an exact match in number of arguments & types of
arguments to invoke a function. If matching function is found then that function
get executed.
2. If there is no exact matching function found, then compiler will perform implicit
conversion (called integral conversion) of actual argument.
Example:
#include<iostream>
using namespace std;
void add(float ,float );
void add(int,int,int);
main()
{
int a=2,b=4,c=6;
add(a,b);
add(a,b,c);
return 0;
}
void add(float a,float b) Output:
{ f1:6
cout<<"f1:"<<a+b<<endl;
return; f2:12
}
Explanation:
In the first function call add(a,b) we are passing two integer variable a & b. but
there is no function having prototype void add(int, int);.
So complier performed implicit conversion of int to float and compiler called the
function having prototype void add(float,float);.
In the 2nd function call add(a,b,c).compiler executed the function having
prototype void add(int, int, int);
Example:
#include<iostream>
using namespace std;
void add(float ,float );
void add(double ,double);
main()
{
int a=2,b=4;
add(a,b);
return 0;
}
void add(float a,float b)
{ Output:
cout<<a+b<<endl;
return;
}
void add(double a,double b)
{
cout<<a+b<<endl;
return;
}
Explanation:
In the function call add(a,b),we are passing two parameter a & b of type int. but
there is no function having prototype void add(int, int );.
so compiler will perform implicit conversion. So conversion ofint to floatand
alsoint to doubleare possible.
So compiler will get confused between two function having prototype void add
(float, float);&void add(double, double);. In this situation compiler will generate
an error.
Precautions with Function Overloading:
Function overloading is powerful feature of C++. But, this facility should not be
overused. Otherwise it becomes an additional overhead.
Following precaution must be taken :
1. Only those functions that basically do the same task, on different sets of
argument , should be overloaded.
Example:
void add(int , int);
void add();
void add(int a,int b)
{
cout<<a+b;
}
void add()
{
cout<<”welcome to RIT”;
CSE/C++/Module-I/Trilochan Rout/Lecture: 14, 15 8
}
NB:
These two functions can be overloaded but should not be overloaded.
2. At least two function having same name needed to be overloaded. in
function overloading more than one function has to be actually defined.
3. Instead of function overloading, use of default arguments makes more
sense.
Using function overloading:
#include<iostream>
using namespace std;
void add(int ,int );
void add(int ,int ,int);
main()
{
int a=2,b=4,c=6;
add(a,b);
add(a,b,c);
return 0;
}
void add(int a,int b)
{
cout<<a+b<<endl;
return;
}
void add(int a,int b,int c)
{
cout<<a+b+c<<endl;
return;
}
Output:
6
12
Using Default Arguments:
#include<iostream>
using namespace std;
void add(int a=0,int b=0 ,int Output: c=0);
main() 6
{
12
int a=2,b=4,c=6;
add(a,b);
add(a,b,c);
return 0;
}
void add(int a,int b,int c)
{
cout<<a+b+c<<endl;
return;
}
Note:
By using function overloading we need to define two function void add (int ,int);
and void add(int, int,int);.
But, by using function with default argument we need to define only one
function void add(int ,int ,int); with default argument. So in this type of case
CSE/C++/Module-I/Trilochan Rout/Lecture: 14, 15 9
4. Declare function prototype before main() function and pass variable instead
of constant directly. This will avoid ambiguity.
Example:
add(2.3,4.7); // avoid this
float a=2.3,b=4.7; // do in this way
add(a,b);