Function Call Stack in C++

Last Updated : 17 Jun, 2026

The function call stack in C++ is a memory structure that manages the execution of functions. Whenever a function is invoked, the information required for its execution is stored on the stack, and it is removed once the function completes.

  • Stores information needed for active functions.
  • Follows the Last In, First Out (LIFO) principle.

Information Stored in a Stack Frame

Whenever a function is called, the system creates a stack frame to store information required for that function's execution. A stack frame typically contains:

  • Function Parameters - Values passed to the function.
  • Local Variables - Variables declared inside the function.
  • Return Address - The location where execution resumes after the function returns.
  • Temporary Data - Additional information needed during function execution.

A stack pointer (SP) keeps track of the top of the stack and is updated whenever stack frames are pushed or popped.

Working of Function Call Stack

The function call stack follows the Last In, First Out (LIFO) principle. When a function is called, a new stack frame is pushed onto the stack. After the function completes execution, its stack frame is removed, and control returns to the calling function.

  1. A function is called.
  2. A stack frame is created and pushed onto the stack.
  3. The function executes using its local variables and parameters.
  4. The function returns.
  5. The stack frame is popped from the stack.
  6. Control resumes at the stored return address.

Example: The following example demonstrates how function calls are managed using the call stack.

C++
#include <iostream>
using namespace std;

void D() {
    float d = 40.5f;
    cout << "In function D\n";
}

void C() {
    double c = 30.5;
    cout << "In function C\n";
}

void B() {
    int b = 20;
    C();
    cout << "In function B\n";
}

void A() {
    int a = 10;
    B();
    cout << "In function A\n";
}

int main() {
    A();
    D();
    return 0;
}

Output
In function C
In function B
In function A
In function D

The below images show how this program is stored in the call stack and how it is executed.

Stack Overflow

Since the call stack has limited memory, repeatedly creating stack frames without removing them can exhaust the available stack space and cause a stack overflow. This commonly occurs due to uncontrolled recursion.

C++
#include <iostream> 
using namespace std; 
void fun() { 
    fun(); 
    
} 

int main() { 
    fun(); 
    return 0; 
    
}

In the above program, the function continuously calls itself without a stopping condition. As new stack frames keep getting added, the call stack eventually runs out of memory, resulting in a stack overflow.

Comment