← Back to C++ Course | Chapter 5: Functions | Lesson 6 of 13

C++ Return Values

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

cpp
#include <iostream>

int square(int n) {
	return n * n;
}

int main() {
	std::cout << square(4) << std::endl;
	return 0;
}

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

cpp
#include <iostream>

void printMessage() {
	std::cout << "Just performs an action" << std::endl;
	return;
}

int main() {
	printMessage();
	return 0;
}

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

cpp
#include <iostream>

bool isValid(int age) {
	return age >= 0 && age <= 120;
}

int main() {
	if (isValid(30)) {
		std::cout << "Valid" << std::endl;
	}
	return 0;
}

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

cpp
#include <iostream>

int globalScore = 0;

int &getScore() {
	return globalScore;
}

int main() {
	getScore() = 42;
	std::cout << globalScore << std::endl;
	return 0;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.