XII - Chapter 3 - Function Overloading
XII - Chapter 3 - Function Overloading
Chapter 3
➤ Function Overloading :
Function overloading is a feature in programming languages that allows multiple functions to have the same
name, but with different parameters or arguments. It is a way to implement polymorphism. For example,
consider the following program in C++:
#include <iostream.h>
using namespace std;
y
int add(int a, int b) {
em
return a + b;
}
ad
return a+b+c;
}
int main() {
cout << add(2, 3) << endl; // calls the int version of add()
ds
return 0;
}
Function overloading provides a way to define multiple functions with the same name but different
parameter lists. The need for function overloading arises from the following scenarios:
CS
1. Multiple operations with the same name: In some cases, we need to perform the same operation on
different data types, such as adding two integers and adding two floating-point numbers. Rather
than defining separate functions with different names for each data type, we can use function
overloading to define a single function with the same name but different parameter lists to perform
the same operation on different data types.
2. Default arguments: In C++, functions can have default arguments that are used when the caller
does not provide a value for that argument. When we need to provide multiple sets of default
arguments for a function, function overloading can be used to define multiple versions of the
function with different default arguments.
3. Code readability and maintainability: Function overloading can improve code readability and
maintainability by allowing functions to have descriptive names that reflect their behavior, rather
2
than needing to create multiple functions with similar but distinct names. This can make code easier
to read and understand, and reduce the likelihood of errors due to confusion or misunderstanding.
4. Adapting to different parameter types: In some cases, we may need to adapt a function to work with
different types of parameters. By overloading the function with different parameter lists, we can
provide different versions of the function that work with different types of parameters, without having
to modify the original function.
y
example :
em
float area(float a) {
return a*a;
}
float area(float a, float b){
return a*b;
ad
}
used to achieve polymorphism, as different objects can be passed as arguments to the same
function, and the appropriate version of the function will be called based on the object types.
Ans: Function overloading is a significant feature in C++ programming language that allows
programmers to define multiple functions with the same name but different parameter lists.
This feature has the following significances:
a. Improved code readability: Function overloading allows programmers to use the same name
for functions that perform similar tasks. This makes the code more readable, as the names
of functions can be more descriptive and meaningful.
A,
b. Code reusability: Function overloading provides a way to reuse code by defining functions
that perform similar tasks but operate on different data types or with different sets of default
arguments. This can simplify the code, as it reduces the amount of duplicate code that
CS
needs to be written.
c. Simplified function calls: With function overloading, programmers can call the same function
name with different parameter lists, depending on the requirements of the specific task. This
simplifies function calls, as the programmer does not need to remember different function
names for similar tasks.
e. Code optimization: Function overloading can improve code optimization by allowing the
compiler to choose the most appropriate version of a function to call, depending on the data
3
types used in the function call. This can result in more efficient code that runs faster and
uses less memory.
5) Rewrite the following program after removing the syntactical error(s) if any? Underline each
correction.
#include<iostream.h>
void main()
y
{
First = 10, Second - 20;
em
jumpto(First,Second);
Jumpto(second);
}
void Jumpto(int N1, int N2 = 20);
{
ad
N1 = N1+N2
count<<N1>>N2;
}
Ans:
Ac
#include<iostream>
void Jumpto(int N1, int N2=20);
void main(){
int First = 10, Second = 20;
ds
Jumpto(First,Second);
Jumpto(Second);
}
Bu