0% found this document useful (0 votes)
15 views12 pages

3 3 FunctionVisualization

A function call uses a program call stack. When a function is called, a stack frame is pushed onto the call stack to store the function's arguments, local variables, and return address. The function's code block is then executed. Once execution returns from the function, the stack frame is popped and its memory is destroyed before returning to the line of code after the call.

Uploaded by

jaiselvamani3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

3 3 FunctionVisualization

A function call uses a program call stack. When a function is called, a stack frame is pushed onto the call stack to store the function's arguments, local variables, and return address. The function's code block is then executed. Once execution returns from the function, the stack frame is popped and its memory is destroyed before returning to the line of code after the call.

Uploaded by

jaiselvamani3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

How a function call works

❖ A function call uses a “program call stack”


❖ A Stack frame is “pushed” when the call is made
❖ Execution jumps to the function’s code block
❖ Function’s code block is executed
❖ Execution returns to just after where call was made
❖ Stack frame is “popped” (variables in it destroyed)
• The code in the function → Steps to solve a
A simple analogy for a problem
stack frame • The arguments to the function → Input
values to the problem
• The function’s stack frame → Rough Sheet
for solving the problem, where you write
down values

• Assume that the problem is of objective


type. You solve the problem in rough
sheet and then copy the result back
• Assume that the institute is in cost-
cutting mode. You can only use pencil to
write. Once done, erase and hand the
rough sheet back, so others can use.
‘main’ is just another function
• Stack frame for ‘main’ is where the values are stored

link
‘main’ is just another function
• See the values of a, b and res

link
An example of using a function sum

link
An example of using a function sum

link
An example of using a function sum

Stack Frame for


sum

link
An example of using a function sum

Values of a & b is
copied into x & y

link
An example of using a function sum

After return, ‘r’ will


be copied into ‘res’
and the Stack Frame
is destroyed

‘r’ is computed.

link
An example of using a function sum

‘res’ is
populated.

link
Another example (nice visualization)

Main’s stack is
empty
In reality, the stack frame looks more like this

Arguments and
return address are
stored before EBP
(base pointer)

All variables
correspond to offset
from a memory
location

You might also like