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

C++ Return Values

A return value is the answer a function hands back when it finishes, like a vending machine giving you a snack. A void function just does its job and returns nothing.
Syntax
cpp
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

cpp
// 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;
}

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 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;
}

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 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;
}

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 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;
}

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting a return statement in a non-void function, which is undefined behavior.
  2. Returning a pointer or reference to a local variable that is destroyed on exit.
  3. Trying to return more than one value with return a, b;, which only returns b.

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.