Topics: Default Arguments, Function Overloading and Recursion
After this lecture, students will be able to:
In the previous lecture, we learned:
In this lecture, we will learn three more useful features of functions.
A default argument is a value provided to a function parameter in advance.
If the programmer does not provide a value for that parameter when calling the function, C++ uses the default value.
Real-life example:
Suppose a university charges Rs. 500 as an application processing fee. If no fee is specified, the system uses Rs. 500 by default.
#include <iostream>
using namespace std;
void displayMessage(string name, int times)
{
for (int i = 1; i <= times; i++)
{
cout << "Welcome " << name << endl;
}
}
int main()
{
displayMessage("Ali", 3);
return 0;
}
Output:
Welcome Ali
Welcome Ali
Welcome Ali
Here, we must provide both arguments when calling the function.
#include <iostream>
using namespace std;
void displayMessage(string name, int times = 1)
{
for (int i = 1; i <= times; i++)
{
cout << "Welcome " << name << endl;
}
}
int main()
{
displayMessage("Ali");
displayMessage("Ahmed", 3);
return 0;
}
Output:
Welcome Ali
Welcome Ahmed
Welcome Ahmed
Welcome Ahmed
Explanation:
displayMessage("Ali"): The function uses the default value 1.displayMessage("Ahmed", 3): The function uses the provided value 3.Correct example:
void calculate(int a, int b = 10, int c = 5);
Incorrect example:
void calculate(int a = 10, int b, int c = 5);
The second example is incorrect because a parameter without a default value follows a parameter with a default value.
#include <iostream>
using namespace std;
int add(int a, int b = 5);
int main()
{
cout << add(10) << endl;
cout << add(10, 20) << endl;
return 0;
}
int add(int a, int b)
{
return a + b;
}
Output:
15
30
Notice that the default value is written in the prototype only.
Function overloading means defining multiple functions with the same name but different parameter lists.
These functions perform similar tasks but can accept different numbers or types of arguments.
Real-life example:
A calculator can add:
We can use the same function name add() for all these operations.
#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int add(int a, int b, int c)
{
return a + b + c;
}
int main()
{
cout << add(10, 20) << endl;
cout << add(10, 20, 30) << endl;
return 0;
}
Output:
30
60
Explanation:
add(10, 20) calls the function with two parameters.add(10, 20, 30) calls the function with three parameters.C++ selects the appropriate function based on the arguments.
#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
int main()
{
cout << add(10, 20) << endl;
cout << add(10.5, 20.5) << endl;
return 0;
}
Output:
30
31
The first function handles integers, while the second handles decimal values.
Invalid example:
int calculate(int a)
{
return a * 2;
}
double calculate(int a)
{
return a * 2.5;
}
This is invalid because both functions have the same name and identical parameter lists. Only their return types are different.
Recursion is a programming technique in which a function calls itself.
A recursive function solves a problem by dividing it into smaller versions of the same problem.
Real-life example:
Imagine a set of nested boxes. Each box contains a smaller box. You continue opening boxes until you reach the smallest box.
Similarly, a recursive function continues calling itself until it reaches a stopping condition.
Every recursive function should have two important parts:
1. Base Case
The condition that stops the recursive calls.
2. Recursive Case
The part where the function calls itself with a smaller or simpler problem.
Without a proper base case, recursion may continue indefinitely until the program runs out of available call stack space.
#include <iostream>
using namespace std;
void count(int n)
{
if (n > 5)
return;
cout << n << endl;
count(n + 1);
}
int main()
{
count(1);
return 0;
}
Output:
1
2
3
4
5
Explanation:
n = 1.n.n + 1.n becomes 6, the base condition becomes true.The following sequence shows how the function works:
Recursive calls
Output: 1, 2, 3, 4, 5
Problem: Write a C++ program to calculate the factorial of a number using recursion.
Factorial means multiplying a positive integer by all positive integers smaller than it.
For example:
\[ 5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 \]
Important:
\[ 0! = 1 \]
C++ Program:
#include <iostream>
using namespace std;
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int number;
cout << "Enter a number: ";
cin >> number;
if (number < 0 || number > 12)
{
cout << "Enter a number between 0 and 12.";
}
else
{
cout << "Factorial = " << factorial(number);
}
return 0;
}
Output:
Enter a number: 5
Factorial = 120
We restrict the input to 12 because larger factorials can exceed the storage capacity of the int data type.
The function works as follows:
factorial(4)
= 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * 2 * factorial(1)
= 4 * 3 * 2 * 1 * factorial(0)
= 4 * 3 * 2 * 1 * 1
= 24
Function call and return process:
| Function Call | Action |
|---|---|
| factorial(4) | Waits for factorial(3) |
| factorial(3) | Waits for factorial(2) |
| factorial(2) | Waits for factorial(1) |
| factorial(1) | Waits for factorial(0) |
| factorial(0) | Returns 1 |
After the base case returns 1, the pending calculations are completed:
This process is called unwinding the recursive calls.
Iteration means repeating instructions using loops such as for and while.
| Recursion | Iteration |
|---|---|
| Function calls itself | Uses loops |
| Requires a base case | Requires a loop condition |
| Uses the call stack for recursive calls | Usually uses less memory |
| Can make some problems easier to express | Often more efficient for simple repetition |
| May be harder to trace | Usually easier to trace for beginners |
Example: Display numbers from 1 to 5
Using a loop:
for (int i = 1; i <= 5; i++)
{
cout << i << endl;
}
Using recursion:
void count(int n)
{
if (n > 5)
return;
cout << n << endl;
count(n + 1);
}
Both approaches produce the same output.
Problem Statement 1: Display Student Information
Write a C++ program using a function with default arguments to display a student’s name and semester.
Tasks:
displayStudent().1.Problem Statement 2: Overloaded Area Function
Write a C++ program using function overloading to calculate areas.
Tasks:
area().Problem Statement 3: Overloaded Maximum Function
Write a C++ program to find the maximum value using overloaded functions.
Tasks:
main().Problem Statement 4: Sum Using Recursion
Write a recursive function to calculate the sum of numbers from 1 to n.
For example:
\[ 1+2+3+4+5=15 \]
Tasks:
sum().Problem Statement 5: Fibonacci Series Using Recursion
Write a C++ program to generate Fibonacci numbers using recursion.
The Fibonacci sequence starts as:
0 1 1 2 3 5 8 13 21
Tasks:
fibonacci().n is 0.n is 1.n terms.return statement in recursion?Test your understanding
0 / 7 answered
1. What is a default argument?
A. A value automatically assigned to a parameter when no argument is supplied
B. A global variable
C. A loop condition
D. A return type
2. Which function calls itself?
A. Overloaded function
B. Recursive function
C. Main function only
D. Void function
3. What is the stopping condition in recursion called?
A. Function prototype
B. Parameter
C. Base case
D. Default argument
4. Can functions be overloaded by changing only their return type?
A. Yes
B. No
C. Only with void
D. Only with int
5. What is the factorial of 0?
A. 0
B. 10
C. 1
D. Undefined
6. Which statement is true about default arguments?
A. They must come before all other parameters
B. They must be assigned from right to left
C. They cannot be used with prototypes
D. Every function must have them
7. Which approach usually uses a function call stack?
A. Recursion
B. A simple for loop
C. A simple while loop
D. An if statement
Submit Answers
In this lecture, we learned three important concepts:
| Topic | Main Idea |
|---|---|
| Default Arguments | Provide predefined values to parameters |
| Function Overloading | Use the same function name with different parameter lists |
| Recursion | A function calls itself until the base case is reached |
Important points to remember: