Returning Values
The function printArea that you wrote earlier in this lesson prints the area, and that's it. But what if we want to keep calculating with the result, for example to add up the area of all the rooms in a flat? A printed value only shows up in the output, and the program can't work with it any further.
That's why functions can return a value. The return statement sends a value back to the place where the function was called. Instead of void, you then write the data type of the value the function returns before its name:
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(2, 3);
std::cout << result << std::endl;
}This program prints:
5The call add(2, 3) runs the function, and the function returns 5. You can imagine that the call is replaced with the value the function returned, so int result = add(2, 3); becomes int result = 5;. The value a function gives back is called its return value, and its type, here int, is the return type. A function can return a value of any type, such as double, bool or std::string.
You can use a function call anywhere you could write a value: print it right away, calculate with it or put it in a condition:
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(2, 3) << std::endl;
int total = add(2, 3) * 2;
std::cout << total << std::endl;
if (add(2, 3) > 4) {
std::cout << "More than 4" << std::endl;
}
}This program prints:
5
10
More than 4In std::cout << add(2, 3), the program first calls add(2, 3) and then prints the number 5 that the function returned. In add(2, 3) * 2, the call becomes 5, and 5 * 2 is 10.
Now you know what the int before main means: main returns a whole number too. It returns it to whoever started the program, and 0 means that the program ended fine. That's why return 0; goes at the end of main. If you leave it out, main returns 0 by itself, but that works only for main, not for any other function. From now on, we will write return 0;.
A common mistake is to forget return in a function other than main. The function promised to return an int, but it returns no value:
#include <iostream>
int add(int a, int b) {
int sum = a + b;
}
int main() {
std::cout << add(2, 3) << std::endl;
return 0;
}The program compiles, but when it calls the function, it stops with an error, such as RuntimeError: unreachable. Below it, you see the compiler's warning, which says why:
RuntimeError: unreachable
main.cpp:5:1: warning: non-void function does not return a value [-Wreturn-type]
5 | }
| ^
1 warning generated.The warning says that a function that isn't void doesn't return a value. Adding return sum; at the end of the function fixes it.
Instructions
- 1
The program doesn't compile: the error
use of undeclared identifier 'rectangleArea'says that the functionrectangleAreadoesn't exist yet. Write it in place of the comment: it has two parameters of typeint,widthandheight, and returnswidth * height. The program then printsKitchen: 12andBedroom: 20. - 2
The flat also has a living room that measures 6 × 5 metres. At the end of
main, beforereturn 0;, create a variablelivingRoomwith its area, calculated with the functionrectangleArea. Then print the area of the whole flat,kitchen + bedroom + livingRoom, with a label:Whole flat: 62.