Uploaded by Adnan Ahmed

Function Adnan

advertisement
Function overloading and Inline functions in C++
ADNAN AHMED
MSCS
COMSATS UNIVERSITY ISB
1
Function overloading
What is overloading
 Overloading means assigning multiples meaning to a function names or operator
 It allows multiple definitions of a function with the same name but different signature
 C++ Supports
 Function overloading
 Operator overloading
2
Function Overloading
 Is the process of using the same name for two or more functions
 Requires each redefinition of a function to use a different function signature that is
◦ Different types of parameter
◦ Or Sequence of parameters
◦ Or number of parameters
 Is used so that a programmer does not have to remember multiple function name
3
Example
Two or more functions having same name but different parameters
Int max (int a, int b)
{
if (a>= b)
{
return a;
}
else
{
return b;
}
Int max (int a, float b)
{
if (a>= b)
{
return a;
}
else
{
return b;
}
}
}
4
Inline function 1/2
 Eliminate the cost of call to small function
 An inline function is a function that is expanded inline when it is invoked
 Inline function must be defined before they are called
 Inline keyword send a request, not a command to the compiler.
5
Inline function 2/2
 Inline function can be implemented using the keyword inline
 The inline is a request to the compiler
 The function always can not be inline by the compiler
 The complicated function will not be inline
 Reduce and save the memory space
6
Syntax
Inline Prototype Function_Name (arguement1 , arguement2)
Inline int add(int x, int y)
7
Restrictions
 Inline function must be simple and small
 If the code size is to large, some compiler will not treat that as inline function
 If the function is not considered as inline function by the compiler,
than it will consider as a normal function
8
When it is not working
 For function returning values
(Loop, Switch or goto )
 If function contain static variable
9
10
Download