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 a function, executes it, returns, then continues main() until it ends.
int foo() { return 42; } int main() { int x = foo(); return 0; }
| Step | Action | Function | Variable State | Output/Return |
|---|---|---|---|---|
| 1 | Start main() | main | x: uninitialized | none |
| 2 | Call foo() | main -> foo | x: uninitialized | none |
| 3 | Execute foo() body | foo | none | return 42 |
| 4 | Return from foo() | foo -> main | x: uninitialized | 42 |
| 5 | Continue main() | main | x: 42 | none |
| 6 | End main() | main | x: 42 | program ends |
| Variable | Start | After Step 4 | Final |
|---|---|---|---|
| x | uninitialized | uninitialized | 42 |
Function calling in C++: - Use functionName() to call a function. - Control jumps to the called function. - The function runs its code and returns a value. - The caller receives the return value and continues. - Variables can store returned values.