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

XII - Chapter 3 - Function Overloading

Function Overloading

Uploaded by

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

XII - Chapter 3 - Function Overloading

Function Overloading

Uploaded by

nikchoudhury1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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;
}

int add(int a, int b, int c){

ad
return a+b+c;
}

double add(double a, double b) {


Ac
return a + b;
}

int main() {
cout << add(2, 3) << endl; // calls the int version of add()
ds

cout << add(2,3) <<endl;


cout << add(2.5, 3.5) << endl; // calls the double version of add()
Bu

return 0;
}

➤ Need for Function Overloading :


A,

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.

➤ Question and Answers :


1) What is function overloading ? (2022) 2
Ans: One function name having several definitions that are differentiable by the number or types of
their arguments, is known as function overloading. It’s a way to implement polymorphism. For

y
example :

em
float area(float a) {
return a*a;
}
float area(float a, float b){
return a*b;

ad
}

2) Explain how polymorphism can be implemented using function overloading ? (2020) 3


Ans: Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that allows
Ac
objects to take on many forms, or to be used in different ways, depending on the context in which
they are being used. One way to implement polymorphism in OOP is through function overloading.
In C++, function overloading allows different functions to have the same name, but with
different parameter lists. When a function is called, the compiler determines which version of the
function to use based on the types and number of arguments passed to it. This behavior can be
ds

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.

3) What is the significance of function overloading in C++?


Bu

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.

d. Type compatibility: Function overloading can improve type compatibility by allowing


programmers to define functions that work with different data types. This makes it easier to
write code that can handle a wide range of data types without needing to write separate
functions for each data type.

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.

4) How would you compare default arguments and function overloading ?


Ans: With default arguments, default values can be provided in the function prototype itself and the
function may be called even if an argument is missing. But there is one limitation with it, if you want
to default a middle argument then all the arguments on its right must also be defaulted.
On the other hand, you can overload a function for all possible argument combinations. It not
only overcomes the limitation of default arguments but also the compiler is saved from the trouble of
testing the default value and pushing it on the function call stack.

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

void Jumpto(int N1, int N2)


{
N1 = N1+N2;
cout<<N1<<" "<<N2;
}
A,
CS

You might also like