“Execution of Functions”
Introduction  Data can be organized in many different ways. For example if we want to store 5 numbers we can store them in five different variables ,an array , a linked list ,etc.  All these different ways of organizing data are known as data structures. The compiler uses one such data structure called “stack” for implementing functions call.
Stack  Stack is a LIFO (Last In First Out) data structure. This means that the last item to get stored on the stack (often called Push operation) is the first one to get out of it (often called Pop operation)  For each function call, there's a section of the stack reserved for the function. This is usually called a stack frame.
… Stack frame of main() looks something like this : It is also called activation record.  A stack frame exists whenever a function has started, but yet to complete.
Suppose, inside of body of main() there's a call to foo(). Suppose foo() takes two arguments. One way to pass the arguments to foo() is through the stack. Thus, there needs to be assembly language code in main() to "push" arguments for foo() onto the stack. The result looks like:
When function is finished.
#include<iostream.h> int add(int i, int j) { int sum; sum=i+j; return sum; } void main() { int a=5,b=2,c; c=add(a,b); cout<<c; }
Calling convention  Calling convention decides whether the parameters being passed to the function are pushed on the stack from left- to-right or from right-to-left. Calling convention also decides that the popping of data from stack is done by which function. Above example used CDecl calling convention.
Execution of functions

Execution of functions

  • 1.
  • 2.
    Introduction  Data canbe organized in many different ways. For example if we want to store 5 numbers we can store them in five different variables ,an array , a linked list ,etc.  All these different ways of organizing data are known as data structures. The compiler uses one such data structure called “stack” for implementing functions call.
  • 3.
    Stack  Stack isa LIFO (Last In First Out) data structure. This means that the last item to get stored on the stack (often called Push operation) is the first one to get out of it (often called Pop operation)  For each function call, there's a section of the stack reserved for the function. This is usually called a stack frame.
  • 4.
    … Stack frame ofmain() looks something like this : It is also called activation record.  A stack frame exists whenever a function has started, but yet to complete.
  • 5.
    Suppose, inside ofbody of main() there's a call to foo(). Suppose foo() takes two arguments. One way to pass the arguments to foo() is through the stack. Thus, there needs to be assembly language code in main() to "push" arguments for foo() onto the stack. The result looks like:
  • 6.
  • 8.
    #include<iostream.h> int add(int i,int j) { int sum; sum=i+j; return sum; } void main() { int a=5,b=2,c; c=add(a,b); cout<<c; }
  • 10.
    Calling convention  Callingconvention decides whether the parameters being passed to the function are pushed on the stack from left- to-right or from right-to-left. Calling convention also decides that the popping of data from stack is done by which function. Above example used CDecl calling convention.