Function Overloading & Passing Arrays to Functions in C++
1. What is Function Overloading? (In Simple Words)
In C language, if you want to calculate the area of a square, circle, and rectangle, you have to create three separate names: areaSquare(), areaCircle(), and areaRectangle().
In C++, you don’t need to create different names! You can use the exact same name area() for all three.
Function Overloading means having two or more functions with the same name, but with different parameters (different types, different counts, or different order).
Real-Life Analogy: Think of a person named “Ramesh”. At home, he acts as a father; in the office, he acts as a manager; on the playground, he acts as a cricket batsman. The person is the same, but his behavior changes depending on who is in front of him!
In C++, having one name with multiple behaviors is called Polymorphism (Compile-Time Polymorphism).
2. Rules for Function Overloading (Exam Golden Rules)
Two functions with the same name can be overloaded if they differ in at least one of these 3 ways:
int add(int a, int b);int add(int a, int b, int c); (Takes 2 vs 3 parameters)float add(float a, float b);int add(int a, int b); (Takes floats vs integers)void show(int a, float b);void show(float a, int b); (Different sequence)⚠️ MOST ASKED EXAM QUESTION:
Can we overload a function based on Return Type alone?
NO, NEVER!
If you write:
int calculate(int x);
float calculate(int x);
The compiler gives an Error! Why? Because if someone callscalculate(10);without storing the answer, the compiler has no way to guess whether you wanted theintorfloatversion.
3. Practical Code Example: Overloaded area()
#include <iostream>
using namespace std;
// 1. Area of a Square (1 integer)
int area(int side) {
return side * side;
}
// 2. Area of a Circle (1 float)
float area(float radius) {
return 3.14159f * radius * radius;
}
// 3. Area of a Rectangle (2 floats)
float area(float length, float breadth) {
return length * breadth;
}
int main() {
cout << "Area of Square (side 5): " << area(5) << endl; // Calls (int)
cout << "Area of Circle (radius 3.5): " << area(3.5f) << endl; // Calls (float)
cout << "Area of Rectangle (4 x 6): " << area(4.0f, 6.0f) << endl; // Calls (float, float)
return 0;
}
4. Passing Arrays to Functions in C++
When you pass an ordinary variable like int x to a function, C++ makes a separate copy of it (Call by Value).
But arrays are different!
When you pass an array to a function, C++ does not copy all elements. Instead, it passes the memory address of the very first element (&arr[0]).
This means passing an array is always Call by Address! If you change any element inside the function, the original array in main() is permanently changed.
#include <iostream>
using namespace std;
// 'int arr[]' receives the memory address of numbers
void doubleElements(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Directly modifies the original array!
}
}
void printArray(const int arr[], int size) { // 'const' protects it from being changed
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
cout << "Original array: ";
printArray(numbers, 5);
doubleElements(numbers, 5); // Pass array name without square brackets
cout << "After doubling: ";
printArray(numbers, 5); // Output: 20 40 60 80 100
return 0;
}
Passing Two-Dimensional (2D) Arrays:
When passing a 2D matrix to a function, the column size is mandatory in the brackets!
void printMatrix(int matrix[][3], int rows) { // Column size [3] MUST be given!
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}
Why must column size be specified?
Because computer memory (RAM) is a single continuous row. The compiler needs to know how many columns are in each row so it knows where Row 1 ends and Row 2 begins!
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- Explain the rules of function overloading. Why can’t we overload by return type alone?
Model Answer:- Functions must differ in number, types, or order of arguments.
- Return type alone is not enough because a caller can execute
myFunc(5);without assigning the return value, making it impossible for the compiler to resolve which function was intended.
- How are arrays passed to functions in C++? Why is column size mandatory for 2D arrays?
Model Answer:- Arrays decay into a pointer to the first element (
&arr[0]), so they are passed by address automatically. - For 2D arrays, memory is stored in row-major continuous sequence. The compiler requires column count
Cto calculate element address:Address = Base + (row * C + col) * sizeof(element).
- Arrays decay into a pointer to the first element (
Frequently Asked 10-Mark Questions:
- What is Polymorphism? Explain Function Overloading as compile-time polymorphism. Write a complete C++ program to calculate the volume of a Cube, Cylinder, and Box using overloaded functions.
Model Answer:- Define Polymorphism (one name, multiple forms) and compile-time binding.
- Write out complete code with formulas:
- Cube:
volume(int s) = s * s * s - Cylinder:
volume(float r, float h) = 3.14159 * r * r * h - Box:
volume(float l, float b, float h) = l * b * h
- Cube:
- Show sample output and explain how the compiler matches arguments at compile time.