C - Function Call - Flow of Control

C Programming

Share

In this tutorial we will learn about flow of control when we call functions in C programming language.

We learned about functions in the functions tutorial and the categories in the functions category tutorial.

When we call or invoke a function the flow of control shifts to the called function from the calling function.

For example, if we call greetings() function from the main() function then the flow of control will move from the main() function to the greetings() function. The flow of control will return back to the main() or calling function after executing all the statements in the greetings() or called function.

Understanding the flow of control

In the following example we have a main(), printSquare(), getSquare() and get10() functions.

#include <stdio.h>

// function declaration
void printSquare(int);
int getSquare(int);
int get10(void);

int main(void) {
  // variable declaration
  int num;
  
  // function call
  num = get10();
  printSquare(num);
  
  return 0;
}

// function definition
void printSquare(int x) {
  int square = getSquare(x);
  printf("Square: %d\n", square);
}

int get10(void) {
  return 10;
}

int getSquare(int n) {
  return n * n;
}

Lets discuss the flow of control for the above code line by line.

First lets create a Function call stack. In this we will keep track of the function call. Initially it will be empty.

Function call stack
EMPTY

Line 1. This line will tell the compiler to get the stdio.h header file in our code.

Line 2. Empty line so, moving on to the next line.

Line 3. Single line comment. This is for us developers and the compiler will ignore it.

Line 4. This line contains the printSquare() function declaration. This function takes an integer value as argument and returns no value.

Line 5. This line contains the getSquare() function declaration. This function takes an integer value as argument and returns an integer value.

Line 6. This line contains the get10() function declaration. This function takes no argument but returns an integer value.

Line 7. Empty line.

Line 8. Starting of the main() function.

Line 9. Single line comment.

Line 10. Variable num of type int is declared. So, the computer will allocate memory space to store integer value and will refer it by the name num.

Line 11. Empty line.

Line 12. Single line comment.

Line 13. Calling the get10() function. At this point the flow of control will move from Line 13 to Line 25 where the get10() function is defined.

Note! Here the main() function is the calling function and the get10() function is the called function.

So, the main() function is calling or invoking the get10() function.

Also note that the computer will remember that it has to return back to Line 13 after finishing with get10() function. So, lets note this returning point.

Function call stack
Return to Line #13

Line 25. Starting of the get10() function.

Line 26. This line contains the return statement and it returns an integer value 10 back to the calling function i.e. main().

Note! At Line 26 we encounter the return statement so, the flow of control will move back to the calling function.

If we check the Function call stack we can see that we have to Return to Line #13.

Function call stack
Return to Line #13

So, we will empty the stack and return to line 13.

Function call stack
EMPTY

Line 13. At this line we assigned the integer value i.e., 10 returned by the get10() function to the variable num.

Line 14. At this line we are making another function call. This time we are calling the printSquare() function and passing the value stored in num variable.

Note! In this case the main() is the calling function and printSquare() is the called function.

So, the flow of control will move from Line 14 to Line 20 where the printSquare() function is defined.

After we are done with the printSquare() function the flow of control will return back to Line 14 so, we will note this in our function call stack.

Function call stack
Return to Line #14

Line 20. Starting of the printSquare() function.

Line 21. At this line we are declaring an int variable by the name square and we are calling the getSquare() function and passing value stored in variable x as a function argument.

This means, the integer value that will be returned by the getSquare() function will be assigned to the variable sqaure.

Since, we are making a function call so, printSquare() function becomes the calling function and getSqaure() function becomes the called function.

So, the flow of control will move from Line 21 to Line 29. And after we are done with the called function the flow of control will return back to Line 21. So, lets note this in our function call stack.

Function call stack
Return to Line #21
Return to Line #14

Line 29. Starting of the getSquare() function.

Line 30. At this line we encounter the return statement.

So, we are computing the square using the * multiplication operator by multiplying the value stored in variable n i.e., n * n which is 10*10 = 100. And the product is returned back to the calling function.

At this point the flow of control will move back to the calling function. So, if we check the function call stack we can see that we have to return back to Line 21.

Function call stack
Return to Line #21
Return to Line #14

So, we will pop that out of the function call stack and will move to line 21.

Function call stack
Return to Line #14

Line 21. We came back to line 21 after making the function call to the getSqaure() function which has returned integer value 100. So, this value is assigned to the variable sqaure.

Line 22. At this line we are printing the square using the printf() function.

Since this is the last line of the printSqaure() function so, the flow of control will return back to the called function from here.

If we look at our function call stack we can see that we have to return to line 14.

Function call stack
Return to Line #14

So, we will return to line 14 and pop this out from our function call stack.

Function call stack
EMPTY

Line 14. We are back to this line after making the function call and there is nothing more to be done here so, we move to the next line.

Line 15. Empty line.

Line 16. We encounter the last line of the main() function which returns 0 and the program execution ends here.

We can summaries the flow of control in the following image.



Share