2. Function Overloading
When the function with the same name perform the
multiple task it is called as the function overloading.
Example: If the function add() can add not only the
integers but also the floats and strings it is
overloaded.
The functions differ in one of the following things
Number of parameters
Data types of parameters
Order of appearance
Compiled By: Kamal Acharya
3. Number Of Arguments
#include<iostream.h>
#include<conio.h>
void print(char);
void print(char, int);
int main()
{
clrscr();
print('X');
print('Z',12);
getch();
return 0;
}
Compiled By: Kamal Acharya
void print(char a)
{
cout<<"In the Single argument
function"<<endl;
cout<<a<<endl;
}
void print(char a, int b)
{
int i;
cout<<"In the double argument
function"<<endl;
for(i=0;i<b;i++)
{
cout<<a<<"t";
}
}
4. Data Types of Parameters
#include<iostream.h>
#include<conio.h>
void add(int , int);
void add(float, float);
int main()
{
clrscr();
add(2,6);
add(12.6,1.2);
getch();
return 0;
}
Compiled By: Kamal Acharya
void add(int a, int b)
{
cout<<"In the integer addition
function"<<endl;
cout<<a+b<<endl;
}
void add(float a, float b)
{
cout<<"In the float addition
function"<<endl;
cout<<a+b<<endl;
}
5. Oreder Of Appearance
#include<iostream.h>
#include<conio.h>
void fun(int , char);
void fun(char, int);
int main()
{
clrscr();
fun(2,'A');
fun('B',1);
getch();
return 0;
}
Compiled By: Kamal Acharya
void fun(int a, char b)
{
cout<<"In the function with first
argument integer and the second
character"<<endl;
cout<<"The first argument is"<<
a<<".The second argument is "<<
b<<endl;
}
void fun(char a, int b)
{
cout<<"In the function with first
argument character and the second
integer"<<endl;
cout<<"The first argument is"<<
a<<".The second argument is "<<
b<<endl;
}
6. Passing Default Arguments
•
Allows programmer to define a default
behavior
A value for a parameter can be
implicitly passed
Reduces need for similar functions
that differ only in the number of
parameters accepted
Compiled By: Kamal Acharya
7. Default parameters must appear after any
mandatory parameters
Bad example
void Trouble(int x = 5, double z, double y) {
...
}
Cannot come before
mandatory parameters
Compiled By: Kamal Acharya
8. Consider
void PrintChar(char c = '=', int n = 80)
{
for (int i = 0; i < n; ++i)
cout << c;
}
What happens in the following invocations?
PrintChar('*', 20);
PrintChar('-');
PrintChar();
Compiled By: Kamal Acharya
9. Program to Illustrate Default Arguments
#include<iostream.h>
#include<conio.h>
void print(int a= 10 , char b='B');
int main()
{
clrscr();
print();
print(2);
print(1,'Z');
getch();
return 0;
}
Compiled By: Kamal Acharya
void print(int a, char b)
{
int i;
for(i=0;i<a;i++)
{
cout<<b<<"t";
}
}
10. Inline Function
Reduce function call overhead—especially for small
functions.
Qualifier inline before a function’s return type in the
function definition
“Advises” the compiler to generate a copy of the
function’s code in place (when appropriate) to avoid
a function call.
Compiled By: Kamal Acharya
11. Multiple copies of the function code are inserted in
the program (often making the program larger).
The compiler can ignore the inline qualifier and
typically does so for all but the smallest functions.
Compiled By: Kamal Acharya
12. Syntax of Inline Function
inline returnType functionName(arguments);
Example:
inline int add(int a, int b);
Compiled By: Kamal Acharya
13. Program to Illustrate inline Function
#include<iostream.h>
#include<conio.h>
inline float mul(float x, float y)
{
return(x*y);
}
inline double div(double p, double
q)
{
return(p/q);
}
Compiled By: Kamal Acharya
int main()
{
clrscr();
float a=12.12;
float b=2.2;
cout<<mul(a,b)<<"n";
cout<<div(a,b)<<"n";
getch();
return 0;
}