Unit 2: Loops, Functions & ScopeLesson #7 / 20

Lesson 7: Function Overloading & Recursion Call Stack Architecture

NSNextSemLast Updated: 5 Aug, 2026

🔄 Overloading & Recursive Call Stacks

C++ supports Function Overloading, allowing multiple functions to share the exact same name as long as their parameter lists (signatures) differ.


🌀 Understanding Recursion & Stack Frames

A Recursive Function is a function that calls itself to solve smaller subproblems until it reaches a Base Case.

Every recursive call creates a new Stack Frame on the CPU call stack storing local variables and return addresses.


💻 Code Example: Overloading & Factorial Recursion

C++ Source Code (main.cpp)
#include <iostream>
// Overloaded Function 1: Integers
int add(int a, int b) { return a + b; }
// Overloaded Function 2: Doubles
double add(double a, double b) { return a + b; }
// Recursive Function: Factorial (n!)
long long factorial(int n) {
if (n <= 1) return 1; // Base Case to prevent Stack Overflow!
return n * factorial(n - 1); // Recursive Call
}
int main() {
std::cout << "Int Add: " << add(5, 10) << std::endl;
std::cout << "Double Add: " << add(4.5, 3.2) << std::endl;
std::cout << "Factorial of 6: " << factorial(6) << std::endl;
return 0;
}
Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

(c) Microsoft Corporation. All rights reserved.

C:\Users\Student\CPP_Project>#include <iostream>

C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp C:\Users\Student\CPP_Project> main.exe Hello World from C++ on Windows 11 Pro! [Process exited with code 0 in 0.001 seconds]

🛠️ Execution Output on ⊞ Windows 11 CMD:

Command Execution Snippet
Int Add: 15
Double Add: 7.7
Factorial of 6: 720
Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

(c) Microsoft Corporation. All rights reserved.

C:\Users\Student\CPP_Project>Int Add: 15

C:\Users\Student\CPP_Project> Int Add: 15 Execution successful! Output verified on Windows 11 Pro x86_64. [Process exited with code 0 in 0.002 seconds]

Interactive Knowledge Check

Test your understanding of Lesson #7 concepts

What is required for C++ compiler to successfully distinguish between overloaded functions?