In C++, the return of a function is the mechanism by which a function can (optionally) return a value to the calling code. This is done using the keyword return
.
The returned value can be of any data type (primitive types, structs, objects…). It is also possible for a function to not return any value, which is indicated with the return type void
.
In general, we can only return a single value directly. But we can return a grouping of several (for example, structs or classes).
If you want to learn more about function returns
check the Introduction to Programming Course read more ⯈
Return of void
When a function does not need to return any value, it is declared with the return type void
. In this case, the function performs an action without providing a result.
#include <iostream>
void Greet() {
std::cout << "Hello!" << std::endl;
}
int main() {
Greet(); // Prints "Hello!"
return 0;
}
Return of a Value
To return a value from a function, the reserved word return
is used. The return type of the function must match the type of the returned value.
Upon reaching a return
statement, the execution of the function stops and control returns to the function that made the call.
#include <iostream>
int Add(int a, int b) {
return a + b;
}
int main() {
int result = Add(3, 4);
std::cout << "The sum is: " << result << std::endl; // Prints "The sum is: 7"
return 0;
}
Return of Multiple Values
Although more than one value cannot be directly returned from a function, there are several techniques to return multiple values in C++:
Using Classes or Structures
A common way is to encapsulate the values in a class or structure and return an instance of that class or structure.
#include <iostream>
struct Result {
int number;
std::string text;
};
Result GetResult() {
return {42, "Example"};
}
int main() {
Result res = GetResult();
std::cout << "Number: " << res.number << ", Text: " << res.text << std::endl; // Prints "Number: 42, Text: Example"
return 0;
}
Returning Collections
Functions can also return collections such as arrays, vectors, lists, or maps.
#include <iostream>
#include <vector>
std::vector<int> GetList() {
return {1, 2, 3, 4, 5};
}
int main() {
std::vector<int> list = GetList();
for (int num : list) {
std::cout << num << " ";
}
std::cout << std::endl; // Prints "1 2 3 4 5"
return 0;
}
Using TuplesC++17 and later
Starting from C++17, tuples can be used to return multiple values. Tuples allow grouping values of different types into a single object.
#include <iostream>
#include <tuple>
std::tuple<int, std::string> GetData() {
return {42, "Example"};
}
int main() {
auto [number, text] = GetData();
std::cout << "Number: " << number << ", Text: " << text << std::endl; // Prints "Number: 42, Text: Example"
return 0;
}
Practical Examples
Example of void
Function
In this example, it shows how to define a function that does not return any value using void
.
#include <iostream>
void ShowMessage(const std::string &message) {
std::cout << message << std::endl;
}
int main() {
ShowMessage("Hello, World!"); // Prints "Hello, World!"
return 0;
}
Example of Function with Value Return
In this example, it shows how to define a function that returns a calculated value, in this case, the area of a circle.
#include <iostream>
#include <cmath> // For the constant M_PI
double CalculateCircleArea(double radius) {
return M_PI * radius * radius;
}
int main() {
double area = CalculateCircleArea(5.0);
std::cout << "The area of the circle is: " << area << std::endl; // Prints "The area of the circle is: 78.5398"
return 0;
}
Example of Function with Tuple Return
In this example, it shows how to define a function that returns a tuple with two calculated values.
#include <iostream>
#include <tuple>
std::tuple<int, int> CalculateSumAndProduct(int a, int b) {
return {a + b, a * b};
}
int main() {
auto [sum, product] = CalculateSumAndProduct(3, 4);
std::cout << "Sum: " << sum << ", Product: " << product << std::endl; // Prints "Sum: 7, Product: 12"
return 0;
}
Example of Function with Class Return
In this example, it shows how to define a function that returns an instance of a class. Here, an instance of the Person
class is created.
#include <iostream>
class Person {
public:
std::string name;
int age;
};
Person CreatePerson(const std::string &name, int age) {
return {name, age};
}
int main() {
Person person = CreatePerson("John", 30);
std::cout << "Name: " << person.name << ", Age: " << person.age << std::endl; // Prints "Name: John, Age: 30"
return 0;
}