C++ return Keyword
The return
keyword in C++ is used to exit a function and optionally pass a value back to the caller. It indicates the end of the function’s execution and can be used with or without a value, depending on the function’s return type.
If a function is declared with a return type other than void
, the return
statement must include a value of the corresponding type. For void
functions, the return
keyword is optional and does not include a value.
Syntax
</>
Copy
// For non-void functions
return value;
// For void functions
return;
- value
- The value to be returned to the caller. This is required for functions with a non-
void
return type.
Examples
Example 1: Returning a Value from a Function
In this example, we shall write a function that returns an integer value.
</>
Copy
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b; // Return the sum of a and b
}
int main() {
int result = add(5, 3); // Call the function and store the return value
cout << "Sum: " << result << endl;
return 0;
}
Output:
Sum: 8
Explanation:
- The
add
function takes two integers as arguments and returns their sum using thereturn
statement. - The
main
function callsadd
, and the returned value is stored in the variableresult
. - The result is then printed to the console.
Example 2: Using return
in a void
Function
In this example, we shall see how return
is used in a void
function to exit early.
</>
Copy
#include <iostream>
using namespace std;
void checkNumber(int num) {
if (num < 0) {
cout << "Negative number detected!" << endl;
return; // Exit the function early
}
cout << "Positive number: " << num << endl;
}
int main() {
checkNumber(-5);
checkNumber(10);
return 0;
}
Output:
Negative number detected!
Positive number: 10
Explanation:
- The
checkNumber
function checks if the input number is negative. - If the number is negative, the function prints a message and exits early using the
return
statement. - If the number is positive, the function continues to execute and prints the positive number.
Example 3: Returning a Value from a Conditional Statement
In this example, we will return a value based on a condition.
</>
Copy
#include <iostream>
using namespace std;
string evaluateGrade(int score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
return "F"; // Default return value
}
int main() {
int score = 85;
cout << "Grade: " << evaluateGrade(score) << endl;
return 0;
}
Output:
Grade: B
Explanation:
- The
evaluateGrade
function returns a grade based on the input score. - The
return
statements inside the conditional blocks return the corresponding grade for each condition. - If none of the conditions are met, the function returns the default grade
"F"
.
Key Points to Remember about return
Keyword
- The
return
keyword is used to exit a function and optionally return a value to the caller. - For non-
void
functions, thereturn
statement must include a value of the appropriate type. - For
void
functions,return
can be used to exit the function early without returning a value. - If a
return
statement is omitted in a non-void
function, the compiler may generate an error or warning.