Using return codes - Time & Space Complexity
When using return codes in C, it's important to see how the program's steps grow as input changes.
We want to know how the number of operations changes when the input size grows.
Analyze the time complexity of the following code snippet.
int processArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
return -1; // error code for negative value
}
}
return 0; // success code
}
This code checks an array for negative values and returns a code indicating success or error.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop through the array elements one by one.
- How many times: Up to n times, where n is the array size.
As the array size grows, the number of checks grows roughly the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows directly with the input size, one check per item.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items to check.
[X] Wrong: "The return statement makes the loop always run only once."
[OK] Correct: The loop stops early only if a negative value is found; otherwise, it checks all items.
Understanding how return codes affect loop execution helps you explain program efficiency clearly and confidently.
"What if the function returned a code only after checking every element, without early return? How would the time complexity change?"
Practice
0 usually mean in C functions?Solution
Step 1: Understand return codes meaning
In C, a return code of 0 typically means success or no error.Step 2: Interpret the code 0
Since 0 means success, the function completed its task without problems.Final Answer:
The function completed successfully -> Option AQuick Check:
Return code 0 = success [OK]
- Thinking 0 means error
- Confusing return code with function output
- Assuming non-zero means success
Solution
Step 1: Identify valid return types for error codes
Return codes are integers, so returning a number like -1 is valid for error.Step 2: Check each option
return 0; returns 0 (success), B returns a multi-char constant (invalid), C returns true (boolean, invalid in C), D returns -1 (valid error code).Final Answer:
return -1; -> Option CQuick Check:
Use integer codes for errors [OK]
- Returning strings instead of integers
- Using boolean values in C return codes
- Returning 0 for errors
#include <stdio.h>
int check(int x) {
if (x > 0) return 0;
else return 1;
}
int main() {
int result = check(-5);
printf("Return code: %d\n", result);
return 0;
}Solution
Step 1: Analyze the check function behavior
If x > 0, it returns 0; otherwise, it returns 1.Step 2: Apply input value -5
Since -5 is not greater than 0, the function returns 1.Final Answer:
Return code: 1 -> Option AQuick Check:
Input -5 <= 0, so return 1 [OK]
- Assuming negative input returns 0
- Confusing return value with input
- Ignoring else branch
int process(int val) {
if (val == 0)
return 0;
else
return;
}Solution
Step 1: Check function return type and return statements
The function returns int, so every return must have an int value.Step 2: Identify missing return value
The else branch has 'return;' without a value, which is invalid for int return type.Final Answer:
Missing return value in else branch -> Option DQuick Check:
int functions must return int always [OK]
- Returning nothing in non-void function
- Confusing void and int return types
- Ignoring compiler warnings
int saveData() that returns 0 on success, 1 if file not found, and 2 if write error. Which is the best way to handle these return codes in main()?Solution
Step 1: Understand return codes purpose
Return codes help main know if saveData succeeded or failed and why.Step 2: Choose proper handling in main
Main should check if return code is 0 (success), else print specific error messages for 1 or 2.Final Answer:
Check if return code is 0, else print error message based on code -> Option BQuick Check:
Use return codes to handle errors clearly [OK]
- Ignoring return codes in main
- Printing success without checking
- Using return codes only for debugging
Ready to prove it?
Pass the test and this pattern moves unknown
