blogger Indonesia

Follow judhyns blog

THE SEQUENCE OF EXECUTION DURING A FUNCTION CALL

Introduction

When the function is called, the current execution is temporarily stopped and the control goes to the called function. After the call, the execution resumes from the point at which the execution is stopped.

To get the exact point at which execution is resumed, the address of the next instruction is stored in the stack. When the function call completes, the address at the top of the stack is taken.

Program

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.

Points to Remember

  1. Functions or sub-programs are implemented using a stack.

  2. When a function is called, the address of the next instruction is pushed into the stack.

  3. When the function is finished, the address for execution is taken by using the pop operation.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |