C++ Return Values
In this page:
return_type function_name(parameters) {
return value;
}
void function_name() {
return; // optional, returns nothing
}
Returning Values
A function sends a result back to its caller with the return statement, and the function's declared return type — like int or double — must match the type of value actually being returned.
Once return executes, the function ends immediately, even if there's more code written after it.
Example: Returning Values
// Include std::cout and std::cin
#include <iostream>
int square(int n) {
// Send n * n back to the caller
return n * n;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << square(4) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Void Return
A void function is declared to return nothing at all, which is appropriate for functions that perform an action (like printing) rather than computing a value to hand back.
You can still write a bare return; with no value inside a void function to exit it early, which is useful for stopping execution partway through based on a condition.
Example: Void Return
// Include std::cout and std::cin
#include <iostream>
void printMessage() {
// Print to the console with cout
std::cout << "Just performs an action" << std::endl;
return;
}
// Program execution starts in main()
int main() {
printMessage();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Boolean Values
Functions that return bool are ideal for yes/no checks, like bool isValid(int age), since the caller can use the result directly in an if condition without any extra comparison.
This pattern makes calling code read almost like plain English: if (isValid(age)).
Example: Returning Boolean Values
// Include std::cout and std::cin
#include <iostream>
bool isValid(int age) {
// Send age >= 0 && age <= 120 back to the caller
return age >= 0 && age <= 120;
}
// Program execution starts in main()
int main() {
// Check whether isValid(30)
if (isValid(30)) {
// Print to the console with cout
std::cout << "Valid" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning References
Returning a reference lets a function hand back direct access to a variable rather than a copy of its value, but this is dangerous if that variable is local to the function — local variables are destroyed the moment the function returns, so a reference to one becomes a dangling reference to memory that no longer exists.
Only return references to data that will outlive the function call, such as a reference parameter or a global variable.
Example: Returning References
// Include std::cout and std::cin
#include <iostream>
// Declare globalScore and set it to 0
int globalScore = 0;
int &getScore() {
// Send globalScore back to the caller
return globalScore;
}
// Program execution starts in main()
int main() {
getScore() = 42;
// Print to the console with cout
std::cout << globalScore << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Returning Structures
When a function needs to hand back more than one piece of related data at once, packing them into a struct and returning that struct as a single unit is cleaner than using multiple output parameters or global variables.
For example, a function computing both a minimum and maximum could return a single struct MinMax { int min; int max; };.
Example: Returning Structures
// Include std::cout and std::cin
#include <iostream>
// Define a structure type named Point
struct Point { int x; int y; };
Point makePoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
// Send p back to the caller
return p;
}
// Program execution starts in main()
int main() {
Point p = makePoint(3, 4);
// Print to the console with cout
std::cout << p.x << "," << p.y << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting a
returnstatement in a non-void function, which is undefined behavior. - Returning a pointer or reference to a local variable that is destroyed on exit.
- Trying to return more than one value with
return a, b;, which only returnsb.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions