You are on page 1of 6

FUNCTION

OVERLOADING
Need

• We may need to defne and call


a number of functons by same
name e.g. area of square/
rectangle/ triangle/ circle etc. In
such cases if we need to
concentrate more on logic instead
of name of functons.
• Functon overloading solves this
problem.
Functon Signature
• A functons argument list i.e.
number and type of arguments is
known as functon signature.
e.g.void sum(int x, int y); void
square(foat a);
Functon Functon Signature Signature

Functon Overloading

• A functon name having several


defnitons tht are diferentable by
the number or types of their
arguments is said to be an
overloaded functon or two/more
functons having same name but
diferent by their signature are
called as overloaded functon and
concept is functon overloading.
Sample Program
#include<iostream.h>

void area(foat r) { cout<<“ Area of circle is: ”<<(2*3.14*r); }

void area (int side) { cout<<“Area of square is: ”<<(side*side); }

void area(int l, int b) { cout<<“Area of rectangle is: ”<<(l*b); }

void main() {
int s, l, b; foat radius; cin>>s>>radius>>l>>b; area(s); area(radius);
area(l, b); }
Calling overloaded
functon
• For calling overloaded functon
compiler goes through the process of
argument matching. A match can be
exact match or it can be through
promoton.
char c; void print(char);
Print(c);
char c; void Exact Exact
print(double); match match Print(c);
Match through
Match through promoton promoton

Default Arguments versus


Overloading
Default arguments gives the appearance
of functon overloading but don’t work as
that. Advantages of functon overloading
over default argument:

• Default argument might not work for all possible


combinatons of arguments whereas functons may
be overloaded for all possible combinatons of
arguments.
e.g. void amount(foat principal, foat rate=0.08, int tme=5); a
functon call with amount(5000, 0.07, 8); is permited but a call
like amount(5000,8); will take rate as 8 i.e. 800%
Middle values can’t be skipped, possible by functon
overloading only.

• With functon overloading multple functon


defnitons can be executed but with default
argument exact one functon defniton is executed.

Restrictons on overloaded
functons
• Signature must be diferent with
same return type and same name of
the functon.

• If return types are diferent with


same name and same signature,
compile tme error.

• If diferent return type with diferent


signature also, two diferent functons
but not overloaded.

You might also like