0% found this document useful (0 votes)
12 views

Unit III - Function call

Uploaded by

pk6048
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Unit III - Function call

Uploaded by

pk6048
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Function Calls in Stack

Overview
• Function call stack in C is a dynamic data structure where elements
are stored at contiguous memory locations. Function call stack is
maintained for every function call where it contains its own local
variables and parameters of the callee function.
• In fact, the function call stack also stores the return address of the
function itself. Function call stack in c is widely used in many
applications like recursion and calling functions.
What are Stacks in C?
• In C, stack is a Linear Data Structure where elements are
stored at contiguous memory locations.
• Stack follows the LIFO mechanism, i.e. Last in First Out. Let’s
understand LIFO mechanism more clearly by an example.
• In tower of Hanoi, all the disks are placed on a peg. It must be placed
on the top of the peg to insert a new disk.
• The top disk must be removed from the peg before removing any other
disk, this is the LIFO mechanism.
What is Call Stack ?
• Call stack is a dynamic data structure
maintained inside the RAM memory by the
Operating System.
• Primary task of Function Call Stack in C is to #include<stdio.h>
manage the function calls and how they pass
void f2() {
parameters to each other. return;
• A call stack is maintained for each task and }

for each thread. It is also called an execution void f1() {


stack or machine stack. More often, it is f2(); //calling f2()
return;
simply known as a stack. }
• Now let’s look at how function calls are //This is main function
actually organized in a stack: Let’s assume int main() {
we have two functions f1() and f2() along f1(); // calling f1()
}
with main().
Activation record: When a function calls another function, an
entry is pushed to the stack. This entry is called as Activation
Record.
• Activation record contains parameters, local variables, and return
address that the called function needs to return to the calling function.
• On running the program, the main() is called, so an activation record
for main() is created and added to the stack.
• Now, main() calls f1(), which creates an activation record for f1() on top of
stack and f1() calls f2() by adding activation record for f2() on top of
stack.
• When f2() terminates, its activation record is removed from the stack.
• After completing the execution of f1(), it returns by removing the
activation record from the stack.
• At this stage, we are back to our main() which removes its activation
record leading to the termination of the program.
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
• In the given diagram of 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.
Sample code for Function call
#include<stdio.h>
//This is fun2() with one parameter
int fun2(int i)
{
int j;
return j;
}
// This is fun1() with two parameters
int fun1(int x, int y)
{
int a;
int b;
b = fun2(a); //calling `fun2()` from fun1()
return b;
}
//This is main() function
int main()
{
int c = fun1(10,30); // calling fun1() from main()
}

You might also like