0
0
Cprogramming~10 mins

Using return codes - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Using return codes
Start main function
Call function
Function runs
Function returns code
Check return code
End program
The program calls a function, gets a return code, then decides what to do based on that code.
Execution Sample
C
#include <stdio.h>

int checkNumber(int n) {
    if (n > 0) return 0;
    else return 1;
}

int main() {
    int result = checkNumber(-5);
    if (result == 0) printf("Positive number\n");
    else printf("Not positive\n");
    return 0;
}
This code checks if a number is positive and returns 0 if yes, 1 if no, then prints a message based on the return code.
Execution Table
StepActionFunction CalledInputReturn CodeCondition CheckedOutput
1Start mainN/AN/AN/AN/AN/A
2Call checkNumbercheckNumber-5N/AN/AN/A
3Inside checkNumbercheckNumber-5N/ACheck if -5 > 0No
4Return from checkNumbercheckNumber-51N/AN/A
5Back in mainN/AN/A1result == 0?No
6Print messageN/AN/A1N/ANot positive
7End programN/AN/AN/AN/AN/A
💡 Program ends after printing message based on return code 1 (not positive).
Variable Tracker
VariableStartAfter checkNumber callFinal
nN/A-5-5
resultN/A11
Key Moments - 3 Insights
Why does the function return 1 when the number is not positive?
Because in the execution_table row 3, the condition 'n > 0' is false, so the else branch returns 1 to indicate 'not positive'.
How does main know if the number is positive or not?
Main checks the return code stored in 'result' (row 5). If it is 0, it means positive; otherwise, not positive.
What happens if the function returns 0?
If return code is 0, main prints 'Positive number' as shown in the logic after row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the return code from checkNumber?
A-5
B0
C1
DN/A
💡 Hint
Check the 'Return Code' column at step 4 in the execution_table.
At which step does main check if the return code means positive number?
AStep 2
BStep 5
CStep 3
DStep 6
💡 Hint
Look at the 'Condition Checked' column to find where 'result == 0?' is evaluated.
If checkNumber returned 0, what would main print?
APositive number
BNot positive
CNo output
DError message
💡 Hint
Refer to the output in step 6 and the condition in step 5.
Concept Snapshot
Using return codes in C:
- Functions return int codes to signal status.
- 0 usually means success or OK.
- Non-zero means error or special condition.
- Main checks return code to decide next steps.
- Use if statements to handle different codes.
Full Transcript
This example shows how a C function returns a code to indicate if a number is positive. The main function calls checkNumber with -5. Inside checkNumber, it checks if the number is greater than zero. Since -5 is not, it returns 1. Back in main, the return code is checked. Because it is 1, main prints 'Not positive'. The program ends after this. This pattern helps programs communicate success or failure between functions.