0
0
C++programming~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 value computed
Return value sent back
Use returned value in caller
When a function finishes, it sends back a value to where it was called, which can then be used.
Execution Sample
C++
int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 4);
    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(3, 4)a=3, b=4N/AFunction starts with parameters 3 and 4
2Calculate a + ba=3, b=47Sum is 7
3Return 7 to callera=3, b=47Function ends, returns 7
4Assign return value to resultresult=7N/AMain stores 7 in result
5End of mainresult=7N/AProgram ends
💡 Function returns value 7, assigned to result, then program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
aN/A333N/AN/A
bN/A444N/AN/A
resultN/AN/AN/AN/A77
Key Moments - 3 Insights
Why does the function stop executing after the return statement?
Because the return statement sends the value back and immediately ends the function, as shown in step 3 of the execution_table.
What happens to the variables inside the function after it returns?
They are discarded after the function ends, so 'a' and 'b' exist only during the function call (steps 1-3).
How does the returned value get used in the main function?
The returned value 7 is assigned to 'result' in step 4, so main can use it after the function call.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value at step 2?
A3
B4
C7
DN/A
💡 Hint
Check the 'Return Value' column at step 2 in the execution_table.
At which step does the function end and return the value?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where the 'Return Value' is sent back and function ends in the execution_table.
If the return statement was missing, what would happen to 'result' in main?
Aresult would be uninitialized or garbage
Bresult would be assigned 7 anyway
Cresult would be assigned 0
DProgram would not compile
💡 Hint
Think about what happens if a function does not return a value but main expects one.
Concept Snapshot
Return values in C++:
- Use 'return' to send a value back from a function.
- Function ends immediately after 'return'.
- Caller receives the returned value to use.
- Variables inside function exist only during call.
- Returned value can be stored or used directly.
Full Transcript
This example shows how a function in C++ returns a value. The function 'add' takes two numbers, adds them, and returns the sum. When 'add(3, 4)' is called, the function runs with a=3 and b=4. It calculates 3+4=7, then returns 7. The main function receives this 7 and stores it in 'result'. The return statement ends the function immediately. Variables inside the function exist only during the call. The returned value is used by the caller after the function ends.