Monday, March 21, 2011

The Sequence Of Execution During A Function Call with Explation

program
void main ( )
{
    printf ("1 \n"); // 1
    printf ("2 \n"); // 2
    printf ("3 \n"); // 3
    printf ("4 \n"); // 4
    printf ("5 \n"); // 5
    f1 ( );
    printf ("6 \n"); // 6
    printf ("7 \n"); // 7
    printf ("8 \n"); // 8
}
void f1 (void)
{
    printf ("f1-9 \n");     // 9
    printf ("f1-10 \n");    // 10
    f2 ( );
    printf ("f1-11 \n");    // 11
    printf ("f1-12 \n");    // 12
}
void f2 (void)
{
    printf ("f2-13 \n");    // 13
    printf ("f2-14 \n");    // 14
    printf ("f3-15 \n");    // 15
}

Explanation

  1. Statements 1 to 5 are executed and function f1( ) is called.
  2. The address of the next instruction is pushed into the stack.
  3. Control goes to function f1( ), which starts executing.
  4. After the 10th statement, fuction f2 is called and address of the next instruction, 11, is pushed into the stack.
  5. Execution begins for function f2 and statements 13, 14, and 15 are executed.
  6. When f2 is finished, the address is popped from the stack. So address 11 is popped.
  7. Control resumes from statement 11.
  8. Statements 11 and 12 are executed.
  9. After finishing the f1 address is popped from the stack, i.e. 6.
  10. Statements 6, 7, and 8 are executed.
  11. The execution sequence is 1 2 3 4 5 f1_9 f1_10 f2_13 f2_14 f2_15 f1_11 f1_12 6 7 8. 

No comments:

Post a Comment