A return value is the result a function sends back to the part of the program that called it. It lets the function give information after finishing its task.
Click to reveal answer
beginner
How do you specify the type of a return value in a C++ function?
You write the type before the function name. For example, int means the function returns an integer number.
Click to reveal answer
intermediate
What happens if a function has a return type but no return statement?
If a function promises to return a value but does not use return, the program may behave unpredictably or cause an error.
Click to reveal answer
intermediate
Can a function return multiple values directly in C++?
No, a function returns only one value directly. To return multiple values, you can use structures, classes, or output parameters.
Click to reveal answer
beginner
What does the void return type mean?
It means the function does not return any value. It just performs actions without sending back a result.
Click to reveal answer
What keyword is used to send a value back from a function in C++?
Areturn
Bsend
Coutput
Dyield
✗ Incorrect
The return keyword is used to send a value back from a function.
If a function is declared as int add(), what must it do?
AReturn a string
BReturn nothing
CReturn an integer value
DPrint output only
✗ Incorrect
The function must return an integer value because its return type is int.
What is the return type of a function that does not return any value?
Aint
Bvoid
Cnull
Dnone
✗ Incorrect
The void return type means the function returns no value.
Which of these can a C++ function return directly?
AOne value
BMultiple values
CNo values and multiple values
DMultiple values without any container
✗ Incorrect
A function can return only one value directly.
What happens if a function with a non-void return type does not have a return statement?
AReturns zero automatically
BReturns a default string
CReturns void
DUndefined behavior or error
✗ Incorrect
Not returning a value when expected causes undefined behavior or errors.
Explain what a return value is and why it is important in C++ functions.
Think about how functions share results with the rest of the program.
You got /3 concepts.
Describe how to handle returning multiple pieces of information from a C++ function.
Consider containers or references to send back more than one value.
You got /3 concepts.
Practice
(1/5)
1. What does the return statement do in a C++ function?
easy
A. It sends a value back to the place where the function was called.
B. It prints a value to the screen.
C. It declares a new variable inside the function.
D. It ends the program immediately.
Solution
Step 1: Understand the purpose of return
The return statement 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; return specifically sends a value back.
Final Answer:
It sends a value back to the place where the function was called. -> Option A
Quick Check:
Return sends value back [OK]
Hint: Remember: return sends value back to caller [OK]
Common Mistakes:
Confusing return with printing output
Thinking return declares variables
Believing return ends the whole program
2. Which of the following is the correct syntax to return an integer value 5 from a function in C++?
easy
A. return = 5;
B. return(5)
C. return 5;
D. return: 5;
Solution
Step 1: Recall correct return syntax
In C++, the correct way to return a value is return value; with a semicolon.
Step 2: Check each option
return 5; uses return 5; which is correct. return(5) misses semicolon. Options C and D use invalid syntax.
Final Answer:
return 5; -> Option C
Quick Check:
Correct return syntax ends with semicolon [OK]
Hint: Use 'return value;' with semicolon [OK]
Common Mistakes:
Omitting semicolon after return
Using '=' with return
Using colon ':' instead of semicolon
3. What will be the output of the following code?
int add() {
return 2 + 3;
}
int main() {
int result = add();
std::cout << result;
return 0;
}
medium
A. 23
B. 5
C. 0
D. Compilation error
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 B
Quick Check:
2 + 3 = 5 [OK]
Hint: Add inside return, then print result [OK]
Common Mistakes:
Thinking return concatenates numbers
Confusing return value with print output
Expecting zero without calculation
4. Identify the error in this function and choose the correct fix:
int multiply(int a, int b) {
a * b;
}
medium
A. Add return before a * b;
B. Change function return type to void
C. Add semicolon after function declaration
D. Replace a * b; with return a + b;
Solution
Step 1: Check function body
The function calculates a * b; but does not return it.
Step 2: Fix missing return
To return the product, add return before a * b;.
Final Answer:
Add return before a * b; -> Option A
Quick Check:
Function must return value if return type is int [OK]
Hint: Return the calculated value explicitly [OK]
Common Mistakes:
Forgetting to use return statement
Changing return type unnecessarily
Confusing multiplication with addition
5. You want a function that returns the larger of two integers. Which of these implementations correctly returns the larger value?
hard
A. int max(int x, int y) { return; }
B. int max(int x, int y) { if (x > y) return y; else return x; }
C. int max(int x, int y) { return x + y; }
D. int max(int x, int y) { if (x > y) return x; else return y; }
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 D
Quick Check:
Return larger value using if-else [OK]
Hint: Return x if greater, else y [OK]
Common Mistakes:
Swapping return values in if-else
Returning sum instead of max
Missing return value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.