0
0
Cprogramming~10 mins

Function calling - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
C
int foo() {
    return 5;
}

int main() {
    int x = foo();
    return 0;
}
Calls function foo() from main(), foo() returns 5, which is stored in x.
Execution Table
StepFunctionActionVariable ChangesReturn Value
1mainStart main()x: uninitializedN/A
2mainCall foo()x: uninitializedN/A
3fooExecute return 5;N/A5
4mainAssign foo() return to xx: 5N/A
5mainReturn 0 and endx: 50
💡 main() returns 0, program ends
Variable Tracker
VariableStartAfter Step 4Final
xuninitialized55
Key Moments - 3 Insights
Why does the program jump to foo() when main() calls it?
When main() calls foo() (see Step 2 in execution_table), the program pauses main() and runs foo() code next.
How does the value 5 get back to main()?
foo() returns 5 (Step 3), which main() receives and stores in x (Step 4).
What happens after foo() finishes?
After foo() returns, the program continues running main() from where it left off (Step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after Step 4?
Auninitialized
B0
C5
DN/A
💡 Hint
Check the 'Variable Changes' column at Step 4 in execution_table.
At which step does foo() return a value to main()?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where 'Return Value' is 5 in execution_table.
If foo() returned 10 instead of 5, what would x be after Step 4?
A10
B5
C0
Duninitialized
💡 Hint
Variable x gets the return value from foo() as shown in variable_tracker and execution_table.
Concept Snapshot
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
Full Transcript
This example shows how a function call works in C. The program starts in main(). When main() calls foo(), the program pauses main() and runs foo() code. foo() returns a value (5), which main() receives and stores in variable x. After foo() finishes, the program continues running main() from where it left off. This process lets us organize code into reusable parts and pass data between them.