Concept Flow - Function calling
Start main()
Call function foo()
Execute foo() body
Return from foo()
Continue main()
End main()
The program starts in main(), calls another function, runs that function's code, then returns to main() to finish.
int foo() { return 5; } int main() { int x = foo(); return 0; }
| Step | Function | Action | Variable Changes | Return Value |
|---|---|---|---|---|
| 1 | main | Start main() | x: uninitialized | N/A |
| 2 | main | Call foo() | x: uninitialized | N/A |
| 3 | foo | Execute return 5; | N/A | 5 |
| 4 | main | Assign foo() return to x | x: 5 | N/A |
| 5 | main | Return 0 and end | x: 5 | 0 |
| Variable | Start | After Step 4 | Final |
|---|---|---|---|
| x | uninitialized | 5 | 5 |
Function calling in C: - main() starts program - main() calls another function by name - Program jumps to called function - Called function runs and returns value - Program resumes main() with returned value - Use return to send data back