How function call works on stack. Understand it the low level language way.

main() calling f1(), f1() calling f2(), f2 calling f3()…

A function calls another function and chain goes on.

When a function is called, A structure named Activation-record or stack-frame is prepared that contains the information associated with it’s call. This information is : local variables, parameters passed in, pointer to the caller function and returning variable(if non-void). That activation-record(structure pointer) of each function is pushed on to a stack called program-stack or run-time-stack.

This is how the architecture of nested function calls is created.

KeyNotes : A limited amount of memory is allocated to stacks(depends on OS) once number of functions exceed(like in case of recursive functions), It hits the memory limit and stack-overflow occurs. You can’t trace the your program’s program-stack at run time.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.