Return values in C++ - Time & Space Complexity
Let's see how the time it takes to run code with return values changes as the input grows.
We want to know how the program's steps increase when returning values from a function.
Analyze the time complexity of the following code snippet.
int sumArray(int arr[], int n) {
int total = 0;
for (int i = 0; i < n; i++) {
total += arr[i];
}
return total;
}
This function adds up all numbers in an array and returns the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that adds each array element to total.
- How many times: It runs once for each element in the array, so n times.
As the array gets bigger, the number of additions grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the size of the input array.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Returning a value makes the function run instantly, no matter the input size."
[OK] Correct: The return itself is quick, but the work done before returning depends on input size, so time grows with input.
Understanding how return values relate to time helps you explain function efficiency clearly and confidently.
"What if the function returned the sum of only the first half of the array? How would the time complexity change?"
Practice
return statement do in a C++ function?Solution
Step 1: Understand the purpose of
Thereturnreturnstatement is used to send a value from a function back to the caller.Step 2: Differentiate from other statements
Printing to screen or declaring variables are different actions;returnspecifically sends a value back.Final Answer:
It sends a value back to the place where the function was called. -> Option AQuick Check:
Return sends value back [OK]
- Confusing return with printing output
- Thinking return declares variables
- Believing return ends the whole program
Solution
Step 1: Recall correct return syntax
In C++, the correct way to return a value isreturn value;with a semicolon.Step 2: Check each option
return 5; usesreturn 5;which is correct. return(5) misses semicolon. Options C and D use invalid syntax.Final Answer:
return 5; -> Option CQuick Check:
Correct return syntax ends with semicolon [OK]
- Omitting semicolon after return
- Using '=' with return
- Using colon ':' instead of semicolon
int add() {
return 2 + 3;
}
int main() {
int result = add();
std::cout << result;
return 0;
}Solution
Step 1: Analyze the add() function
The function returns the sum of 2 + 3, which is 5.Step 2: Check main() output
Main calls add(), stores 5 in result, then prints it using std::cout.Final Answer:
5 -> Option BQuick Check:
2 + 3 = 5 [OK]
- Thinking return concatenates numbers
- Confusing return value with print output
- Expecting zero without calculation
int multiply(int a, int b) {
a * b;
}Solution
Step 1: Check function body
The function calculatesa * b;but does not return it.Step 2: Fix missing return
To return the product, addreturnbeforea * b;.Final Answer:
Addreturnbeforea * b;-> Option AQuick Check:
Function must return value if return type is int [OK]
- Forgetting to use return statement
- Changing return type unnecessarily
- Confusing multiplication with addition
Solution
Step 1: Understand the goal
The function should return the larger of x and y.Step 2: Check each option logic
int max(int x, int y) { if (x > y) return x; else return y; } returns x if x > y, else y, which is correct. int max(int x, int y) { if (x > y) return y; else return x; } reverses logic. int max(int x, int y) { return x + y; } returns sum, not max. int max(int x, int y) { return; } returns nothing.Final Answer:
int max(int x, int y) { if (x > y) return x; else return y; } -> Option DQuick Check:
Return larger value using if-else [OK]
- Swapping return values in if-else
- Returning sum instead of max
- Missing return value
Ready to prove it?
Pass the test and this pattern moves unknown
