0
0
Cprogramming~10 mins

Return values in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Return values
Function called
Execute function body
Return statement reached
Return value sent back
Function call replaced by return value
When a function is called, it runs its code and sends back a value using return, which replaces the function call.
Execution Sample
C
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(2, 3);
    return 0;
}
This code defines a function that adds two numbers and returns the sum, which is stored in 'result'.
Execution Table
StepActionVariablesReturn ValueNotes
1Call add(2, 3)a=2, b=3N/AFunction starts with parameters a=2, b=3
2Calculate a + ba=2, b=3N/ASum is 5
3Return 5a=2, b=35Return value 5 sent back to caller
4Assign return to resultresult=5N/AVariable 'result' now holds 5
5End mainresult=5N/AProgram ends
💡 Function returns value 5, which is assigned to 'result', then program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aN/A222N/A
bN/A333N/A
resultundefinedundefinedundefinedundefined5
Key Moments - 2 Insights
Why does the function stop running after the return statement?
Because return immediately sends the value back and exits the function, no code after return runs (see Step 3 in execution_table).
What happens to the return value after the function finishes?
The return value replaces the function call in the calling code and can be stored in a variable like 'result' (see Step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value at Step 3?
A5
B2
C3
DUndefined
💡 Hint
Check the 'Return Value' column at Step 3 in the execution_table.
At which step is the variable 'result' assigned a value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Variables' and 'Notes' columns in the execution_table for when 'result' changes.
If the return statement was missing, what would happen to the return value?
AIt would be 0 by default
BThe function would return 5 anyway
CThe function would return garbage or undefined
DThe program would crash
💡 Hint
In C, missing return in non-void functions leads to undefined return value.
Concept Snapshot
Return values in C:
- Use 'return' to send a value back from a function.
- The function stops running after return.
- The returned value replaces the function call.
- Store it in a variable to use later.
- Missing return in non-void functions causes undefined behavior.
Full Transcript
This example shows how a function in C returns a value. When 'add' is called with 2 and 3, it calculates the sum and returns 5. The return statement ends the function immediately. The returned 5 replaces the function call in 'main' and is stored in 'result'. Variables 'a' and 'b' hold the input values during the function execution. Beginners often wonder why code after return doesn't run; it doesn't because return exits the function. Also, the return value is what the caller receives and can use. If a return is missing in a function that should return a value, the result is unpredictable.