Open In App

Function Call Stack in C

Last Updated : 20 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A function in C is a set of code that performs a specific task and can be used whenever needed just by calling it. But how the function call works and how the data of the function is stored in the memory is an area of interest for a better understanding of the concepts such as recursion.

What is a Call Stack in C?

A call stack in C is the stack that holds all the function calls, with the bottom elements as the main function. It contains information of the active functions of the program. It is also called the program stack.

What happens when we call a Function?

Before understanding the working of a function call, we need some prerequisite knowledge about Program Execution in the CPU, Stack, Stack Pointer, and Stack Frame.

Stack

The stack is a linear data structure that follows the LIFO(Last In First Out) or FILO(First In Last Out) policy of data insertion and deletion. It can be visualized as a group of elements stacked over one another and only the top element is accessible.

Stack Pointer

The stack pointer is the pointer that points to the top of the stack. It will always point to the stack frame of the function currently being executed.

Stack Frame

A stack frame is the buffer memory that is the element of the call stack. It is a collection of all the local variables, function parameters, return address, and all the data related to the called function.

Working of the Function Call

Now, whenever a function is called, a new stack frame is created with all the function’s data, and this stack frame is pushed into the program stack, and the stack pointer that always points to the top of the program stack points to the stack frame pushed as it is on the top of the program stack.

The series of operations when we call a function are as follows:

  1. Stack Frame is pushed into the stack.
  2. Sub-routine instructions are executed.
  3. Stack Frame is popped from the stack.
  4. Now Program Counter is holding the return address.

Note: POP instruction in assembly language removes the top of the stack and assigns it to the program counter.

Example of Function Call

call stack example

 

In the above instance of calling a function.

  1. Program counterpoints to the next instruction memory location i.e., 104 before executing a function whereas 100 is the memory location of the calling function.
  2. To restore the return address, the contents of the Program Counter are pushed into the stack. Now address stored at the top of the stack is 104.
  3. Calling function Execution: Now Program Counterpoints to 2000 which is the starting address of the subroutine. After execution of all successive instructions in the subroutine, the address is popped from the stack.
  4. Popping off the stack refers to removing the top of the stack and assigning it to the Program Counter. Now Program Counter is holding the return address i.e., 104.

Conclusion

The main motive behind this article is to understand the reason behind pushing the return address in the stack. However only pushing of return address into the stack is not sufficient in the modern implementation of a function. We need to push the actual and formal parameters before pushing the return address.

FAQs on C Call Stack

1. What is a stack frame?

Stack Frame is actually a buffer memory that is an element of the program stack and has data of the called function, i.e.:

  • Return Address
  • Input Parameter
  • Local Variables
  • Register Savings

2. What happens when we call a function?

Whenever a function is called a new stack frame is created with all the function’s data, and this stack frame is pushed into the program stack, and the stack pointer that always points to the top of the program stack points to the stack frame pushed as it is on the top of the program stack.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads