SlideShare a Scribd company logo
FUNCTION OVERLOADING
Prof. K. Adisesha
Learning Outcomes
 Introduction
 Defining Function
 Function overloading
 Calling Overloaded Functions
 Inline Function
 Friend Function
2
Introduction
Function
 User defined function is a function defined by the user to
solve his/her problem.
 Such a function can be called from anywhere and any
number of times in the program.
 C++ implements polymorphism through function
overloading and operator overloading.
 Function overloading allows the user to create new
abstract data type.
3
Function Overloading
Function Overloading:
 Function Overloading means two or more
functions have same name, but differ in the
number of arguments or data types of
arguments.
 Function overloading is the process of defining
same function name to carry out similar types
of activities with various data items.
4
Function Overloading
Definition and Declaration of overloaded functions:
 The main factor in function overloading is a functions
argument list.
 If functions having same name with different types of
arguments or different number of arguments, then it is
invoked automatically by the compiler.
 Function Overloading is also known as Compile time
polymorphism.
 Example:
 int sum (int a, int b)
 float sum ( float p, float q);
. 5
Function Overloading
 To overload a function, each overloaded function must be
declared and defined separately.
 Example:
int product ( int p, int q, int r); //Definition of overloaded functions
float product ( float x, float y);
int product ( int p, int q, int r) //Declaration of overloaded functions
{
cout<<”Product = “<<p * q * r << endl;
}
float product ( float x, float y, float z);
{
cout<< “Product = “ << x * y <<endl;
}
6
Function Overloading
Need for function overloading:
 The advantage of function overloading are:
 Code is executed faster.
 It is easier to understand the flow of information
and debug.
 Code Maintenance is easy.
 Easier interface between programs and real world
objects.
7
Inline Function
Inline function:
 An Inline function is a special type of function whose body is
inserted at the place where it is called, instead of transferring
the control to the function.
 The keyword inline is used to define inline function.
 Rules:
 Inline function definition starts with keyword inline.
 The inline function should be defined before all function
that call it.
 The compiler replaces the function call statement with the
function code itself and then compiles the entire code.
8
Inline Function
Inline function:
 The general format for the inline function declaration is given
below.
 Syntax:
inline Returntype Fun_Name ( [Argument] )
{ ……….. ;
return expression;
}
 Example:
9
#include<iostream.h>
inline int cube( int a )
{
return a * a * a;
}
void main( )
{
cout<<“Cube of 3 is”<<cube(3);
return 0;
}
Inline Function
Advantage of inline function:
 The size of the object code is considerably reduced.
 The speed of execution of a program increases.
 Very efficient code can be generated.
 There is no burden on the system for function calling.
 It also saves the overhead of return call from a function.
 The readability of the program increases.
10
Inline Function
Disadvantage of inline function:
 May increase the size of the executable file
 More memory is needed.
 If used in header file, it will make your header file size
large and may also make it unreadable
The inline function may not work some times for one of
the following reasons:
 The inline function definition is too long or too complicated.
 The inline function is recursive.
 The inline function has looping constructs.
 The inline function has a switch or goto statement.
11
Friend Function
 A friend function is a non-member function of a class has
the access permission to the private member of the class.
 The friend function is declared within a class with the
prefix friend.
 But it should be defined outside the class like a normal
function without the prefix friend.
 The general format for the friend function is given below:
class class_name
{
public:
friend return_type function_name ( [arguments] );
}
12
Friend Function
Properties of friend functions:
 Friend function is a non-member function of the class, has full access
permission to members of the class.
 It can be declared either in public or private part of a class.
 A friend function cannot be called using the object of that class. It can be
invoked like any normal function.
 The function is declared with keyword friend. But while defining friend
function it does not use (: :) operator.
 They are normal external functions that are given special access privileges.
 It cannot access the data member variables directly and has to use an:
objectname.membername.
 Use of friend function is rare, since it violates the rule of encapsulation and
data hiding.
13
Friend Function
#include<iostream.h>
#include<conio.h>
class number
{
private:
int a;
public:
void readdata( )
{
cout<<”Enter the Number”<<endl;
cin>>a;
}
friend int even(number);
};
int even(number n)
{
if(n.a % 2 = = 0)
return 1;
else
return 0;
}
void main( )
{
number num1;
num1.readadata( );
if( even(num1) ) //friend function call
cout<<”Number is Even”;
else
cout<<’Number is Odd”;
}
14
Program to check a number is even or odd using a friend function
Discussion
Important Questions:
 What is function overloading? Explain the need for function
overloading.
 Discuss overloaded functions with syntax and example.
 What is inline function? Write a simple program for it.
 Mention the advantage and disadvantage of inline function.
 Explain friend function and their characteristics.
 Program to check whether a number is prime or not using
inline function.
15

More Related Content

Function overloading

  • 2. Learning Outcomes  Introduction  Defining Function  Function overloading  Calling Overloaded Functions  Inline Function  Friend Function 2
  • 3. Introduction Function  User defined function is a function defined by the user to solve his/her problem.  Such a function can be called from anywhere and any number of times in the program.  C++ implements polymorphism through function overloading and operator overloading.  Function overloading allows the user to create new abstract data type. 3
  • 4. Function Overloading Function Overloading:  Function Overloading means two or more functions have same name, but differ in the number of arguments or data types of arguments.  Function overloading is the process of defining same function name to carry out similar types of activities with various data items. 4
  • 5. Function Overloading Definition and Declaration of overloaded functions:  The main factor in function overloading is a functions argument list.  If functions having same name with different types of arguments or different number of arguments, then it is invoked automatically by the compiler.  Function Overloading is also known as Compile time polymorphism.  Example:  int sum (int a, int b)  float sum ( float p, float q); . 5
  • 6. Function Overloading  To overload a function, each overloaded function must be declared and defined separately.  Example: int product ( int p, int q, int r); //Definition of overloaded functions float product ( float x, float y); int product ( int p, int q, int r) //Declaration of overloaded functions { cout<<”Product = “<<p * q * r << endl; } float product ( float x, float y, float z); { cout<< “Product = “ << x * y <<endl; } 6
  • 7. Function Overloading Need for function overloading:  The advantage of function overloading are:  Code is executed faster.  It is easier to understand the flow of information and debug.  Code Maintenance is easy.  Easier interface between programs and real world objects. 7
  • 8. Inline Function Inline function:  An Inline function is a special type of function whose body is inserted at the place where it is called, instead of transferring the control to the function.  The keyword inline is used to define inline function.  Rules:  Inline function definition starts with keyword inline.  The inline function should be defined before all function that call it.  The compiler replaces the function call statement with the function code itself and then compiles the entire code. 8
  • 9. Inline Function Inline function:  The general format for the inline function declaration is given below.  Syntax: inline Returntype Fun_Name ( [Argument] ) { ……….. ; return expression; }  Example: 9 #include<iostream.h> inline int cube( int a ) { return a * a * a; } void main( ) { cout<<“Cube of 3 is”<<cube(3); return 0; }
  • 10. Inline Function Advantage of inline function:  The size of the object code is considerably reduced.  The speed of execution of a program increases.  Very efficient code can be generated.  There is no burden on the system for function calling.  It also saves the overhead of return call from a function.  The readability of the program increases. 10
  • 11. Inline Function Disadvantage of inline function:  May increase the size of the executable file  More memory is needed.  If used in header file, it will make your header file size large and may also make it unreadable The inline function may not work some times for one of the following reasons:  The inline function definition is too long or too complicated.  The inline function is recursive.  The inline function has looping constructs.  The inline function has a switch or goto statement. 11
  • 12. Friend Function  A friend function is a non-member function of a class has the access permission to the private member of the class.  The friend function is declared within a class with the prefix friend.  But it should be defined outside the class like a normal function without the prefix friend.  The general format for the friend function is given below: class class_name { public: friend return_type function_name ( [arguments] ); } 12
  • 13. Friend Function Properties of friend functions:  Friend function is a non-member function of the class, has full access permission to members of the class.  It can be declared either in public or private part of a class.  A friend function cannot be called using the object of that class. It can be invoked like any normal function.  The function is declared with keyword friend. But while defining friend function it does not use (: :) operator.  They are normal external functions that are given special access privileges.  It cannot access the data member variables directly and has to use an: objectname.membername.  Use of friend function is rare, since it violates the rule of encapsulation and data hiding. 13
  • 14. Friend Function #include<iostream.h> #include<conio.h> class number { private: int a; public: void readdata( ) { cout<<”Enter the Number”<<endl; cin>>a; } friend int even(number); }; int even(number n) { if(n.a % 2 = = 0) return 1; else return 0; } void main( ) { number num1; num1.readadata( ); if( even(num1) ) //friend function call cout<<”Number is Even”; else cout<<’Number is Odd”; } 14 Program to check a number is even or odd using a friend function
  • 15. Discussion Important Questions:  What is function overloading? Explain the need for function overloading.  Discuss overloaded functions with syntax and example.  What is inline function? Write a simple program for it.  Mention the advantage and disadvantage of inline function.  Explain friend function and their characteristics.  Program to check whether a number is prime or not using inline function. 15