C++ Return Values
In this page:
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 <iostream>
int square(int n) {
return n * n;
}
int main() {
std::cout << square(4) << std::endl;
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 <iostream>
void printMessage() {
std::cout << "Just performs an action" << std::endl;
return;
}
int main() {
printMessage();
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 <iostream>
bool isValid(int age) {
return age >= 0 && age <= 120;
}
int main() {
if (isValid(30)) {
std::cout << "Valid" << std::endl;
}
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 <iostream>
int globalScore = 0;
int &getScore() {
return globalScore;
}
int main() {
getScore() = 42;
std::cout << globalScore << std::endl;
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 <iostream>
struct Point { int x; int y; };
Point makePoint(int x, int y) {
Point p;
p.x = x;
p.y = y;
return p;
}
int main() {
Point p = makePoint(3, 4);
std::cout << p.x << "," << p.y << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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