1. Intro
The return statement immediately ends the execution of a function and, optionally, sends a value back to the code that called it. What the return statement is allowed to send back depends entirely on the function's declared return type.
Cricket analogy: When an umpire signals "out", play stops immediately for that batter and the decision is sent back to the scorer, just as return immediately ends a function and can send a value back to the caller, matching what the function promised to deliver.
2. Syntax
returnType functionName(parameterList) {
// statements
return value; // value's type must match returnType
}
// for a function with no return value:
void functionName(parameterList) {
// statements
return; // optional, just exits the function
}3. Explanation
When a function's return type is something like int, double, or string, its return statement must supply a value of a compatible type — this value is handed back to the caller and can be stored, printed, or used in an expression. When a function is declared void, it means the function does not return any value; its return statement (if used at all) takes no value and is simply used to exit the function early, for example inside an if block. Execution of a function stops the instant a return statement runs — any code written after it in the same block is never executed. A function can contain multiple return statements (e.g., in different branches of an if-else), but only one of them executes on any given call.
Cricket analogy: A function returning int must hand back a real run total like return 87, usable by the caller; a void function like announceOver() just exits early with a bare return inside an if, and once a boundary-hit branch returns, no code after it in that over's logic runs — multiple returns can exist across different innings-state branches, but only one fires per call.
A non-void function must return a value on every possible execution path. If a code path in a value-returning function has no return statement, the behavior is undefined and most compilers will warn you — always ensure every branch returns something.
4. Example
#include <iostream>
using namespace std;
int checkSign(int n) {
if (n > 0) {
return 1; // positive
} else if (n < 0) {
return -1; // negative
}
return 0; // zero
}
void printGreeting(bool morning) {
if (!morning) {
cout << "Good evening!" << endl;
return; // exits early, no value returned
}
cout << "Good morning!" << endl;
}
int main() {
cout << "Sign of -7: " << checkSign(-7) << endl;
printGreeting(false);
return 0;
}5. Output
Sign of -7: -1
Good evening!6. Key Takeaways
- The return statement ends a function's execution immediately.
- A function's return type determines what kind of value (if any) return can send back.
- A void function returns no value; its return statement (if present) takes no value.
- Code written after a return statement in the same block never executes.
Practice what you learned
1. What does the return statement do when executed inside a function?
2. Which return type indicates that a function does not return any value?
3. What happens to code written immediately after a return statement in the same block?
4. Can a function have more than one return statement?
5. What is required in every possible execution path of a non-void function?
Was this page helpful?
You May Also Like
Functions in C++
Learn what a function is in C++, why programs are broken into functions, and how reusability, modularity, and readability improve with them.
Function Parameters and Arguments in C++
Learn the difference between function parameters and arguments in C++ and understand pass-by-value semantics with a hands-on example.
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics