0% found this document useful (0 votes)
6 views

Function-Overloading

COMPUTER SCIENCE

Uploaded by

Park Jiminshii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Function-Overloading

COMPUTER SCIENCE

Uploaded by

Park Jiminshii
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Function Overloading:

Function overloading is a feature of object-oriented programming where two or more


functions can have the same name but different parameters. When a function name is
overloaded with different jobs it is called Function Overloading. In Function Overloading
“Function” name should be the same and the arguments should be different.
In other words, Function Overloading, if multiple functions having same name but
parameters of the functions should be different. If we have to perform only one operation and
having same name of the functions increases the readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number of
arguments, if you write the function such as a(int,int) for two parameters, and b(int,int,int) for
three parameters then it may be difficult for you to understand the behavior of the function
because its name differs.
Function overloading allows you to define multiple functions with the same name but different
parameters.
The parameters should follow any one or more than one of the following conditions for
Function overloading:
 Parameters should have a different type
sum(int a, int b)
sum(double a, double b)

Example
#include <iostream>
using namespace std;
void sum(int a, int b)
{
cout << "sum = " << (a + b);
}
Void sum(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
// Driver code
int main()
{
sum(10, 5);
sum(5.3, 5.2);
return 0;
}
Output
sum = 15
sum = 10.5

 Parameters should have a different number


sum(int a, int b)
sum(int a, int b, int c)
Example
#include <iostream>
using namespace std;
void sum(int a, int b)
{ cout << "sum = " << (a + b);}
void sum(int a, int b, int c)
{
cout << endl << "sum = " << (a + b + c);
}
// Driver code
int main()
{ sum(10, 2);
sum(5, 6, 4);
return 0;
}

Output
sum = 12
sum = 15

 Parameters should have a different sequence of parameters.


Parameters with int and double
sum(int a, double b)
sum(double a, int b)
Example
#include<iostream>
using namespace std;
void sum(int a, double b)
{
cout<<"sum = "<<(a+b);
}
void sum(double a, int b)
{ cout<<endl<<"sum = "<<(a+b);
}
// Driver code
int main()
{ sum(10,2.5);
sum(5.5,6);
return 0;
}
Output
sum = 12.5
sum = 11.5

Benefits of Function Overloading


 Code Readability: Allows use of the same function name, which makes code more intuitive.
 Type Safety: The compiler enforces which overloaded function to call, reducing potential
errors.

#include <iostream>
using namespace std;

void myFunction();
void myFunction(int);

int main()
{
myFunction();

int num;
cout<<"Enter a number: ";
cin>>num;

myFunction(num);
cout<<endl;
return 0;
}
void myFunction()
{
cout<<"---Welcome to the Portal---\n";
}
void myFunction(int x)
{
cout<<"You entered: "<<x;
}

Functions with Default Parameters


This section discusses functions with default parameters. Recall that when a function is
called, the number of actual and formal parameters must be the same. C++ relaxes this
condition for functions with default parameters. You specify the value of a default parameter
when the function name appears for the first time, such as in the prototype.
In general, the following rules apply for functions with default parameters:
• If you do not specify the value of a default parameter, the default value is used for that
parameter.
• All of the default parameters must be the far-right parameters of the function.
• Suppose a function has more than one default parameter. In a function call, if a value to a
default parameter is not specified, then you must omit all of the arguments to its right.
• Default values can be constants, global variables, or function calls.
• The caller has the option of specifying a value other than the default for any default
parameter.
• You cannot assign a constant value as a default value to a reference parameter.

Consider the following function prototype:


void funcExp(int x, int y, double t, char z = 'A', int u = 67, char v = 'G', double w =
78.34);

The function funcExp has seven parameters. The parameters z, u, v, and w are default
parameters. If no values are specified for z, u, v, and w in a call to the function funcExp, their
default values are used.
Suppose you have the following statements: int a, b; char ch; double d;
The following function calls are legal:
1. funcExp(a, b, d);
2. funcExp(a, 15, 34.6, 'B', 87, ch);
3. funcExp(b, a, 14.56, 'D');

In statement 1, the default values of z, u, v, and w are used.


In statement 2, the default value of z is replaced by 'B', the default value of u is replaced by
87, the default value of v is replaced by the value of ch, and the default value of w is used.
In statement 3, the default value of z is replaced by 'D', and the default values of u, v, and w
are used.
The following function calls are illegal:
1. funcExp(a, 15, 34.6, 46.7);
2. funcExp(b, 25, 48.76, 'D', 4567, 78.34);

In statement 1, because the value of z is omitted, all other default values must be omitted.
In statement 2, because the value of v is omitted, the value of w should be omitted, too.
The following are illegal function prototypes with default parameters:
1. void funcOne(int x, double z = 23.45, char ch, int u = 45);
2. int funcTwo(int length = 1, int width, int height = 1);
3. void funcThree(int x, int& y = 16, double z = 34);
In statement 1, because the second parameter z is a default parameter, all other parameters
after z must be default parameters.
In statement 2, because the first parameter is a default parameter, all parameters must be
the default parameters.
In statement 3, a constant value cannot be assigned to y because y is a reference
parameter.

#include<iostream>
using namespace std;
int volume(int l = 1, int w = 1, int h = 1);
void funcOne(int& x, double y = 12.34, char z = 'B');
int main()
{
int a = 23; double b = 48.78; char ch = 'M';
cout << " a = " << a << ", b = " << b << ", ch = " << ch << endl;
cout << " Volume = " << volume() << endl;
cout << " Volume = " << volume(5, 4) << endl;
cout << " Volume = " << volume(34) << endl;
cout << " Volume = " << volume(6, 4, 5) << endl;
funcOne(a); funcOne(a, 42.68); funcOne(a, 34.65, 'Q');
cout << " a = " << a << ", b = " << b << ", ch = " << ch << endl;
return 0;
}
int volume(intl,intw,inth)
{
returnl*w*h;
}
void funcOne(int &x,double y,char z)
{
x =2*x;
cout<<"x="<<x<<",y=" <<y<<",z="<<z<<endl;
}

You might also like