Print or Return?
Beginners often mix up return and std::cout, because both seem to give a value out. But they do very different things:
std::coutonly shows a value to you, in the output. The program can't work with what was printed.returngives the value to the program, which can store it in a variable, calculate with it or print it.
Look at what happens when a function prints its result instead of returning it, and the program then wants to calculate with it:
#include <iostream>
void printSum(int a, int b) {
std::cout << a + b << std::endl;
}
int main() {
int result = printSum(2, 3);
std::cout << result * 2 << std::endl;
return 0;
}The program doesn't compile:
main.cpp:8:9: error: cannot initialize a variable of type 'int' with an rvalue of type 'void'
8 | int result = printSum(2, 3);
| ^ ~~~~~~~~~~~~~~
1 error generated.The error message says that a variable of type int can't be filled with a value of type void. The function printSum is void, so it returns nothing that could be stored. It would only print the number 5. printSum(2, 3) * 2 ends the same way, only with the error invalid operands to binary expression ('void' and 'int'): you can't calculate with “nothing”.
The fix is to change void to int and return the value instead of printing it: return a + b;.
It can also happen the other way round: a function returns a value, but nobody uses it. If you write only add(2, 3); in main, the program prints nothing. The function returned 5, but the program didn't store it or print it, so the value was lost. If you want to see it, print it: std::cout << add(2, 3) << std::endl;.
So when should a function print, and when should it return? A function that calculates something should return the result. The code that calls it then decides what to do with it: print it, store it or keep calculating with it. Print inside a function only when printing is its whole job, like in sayHello() or printArea().
You can return text too. A function that prepares a text with a number in it can return it as a std::string, and the program then prints it wherever it wants. But you can't attach a number to a text with +. First turn it into text with the function std::to_string():
#include <iostream>
#include <string>
std::string formatPrice(int price) {
return std::to_string(price) + " EUR";
}
int main() {
std::string text = formatPrice(120);
std::cout << "Price: " << text << std::endl;
std::cout << "With a discount: " << formatPrice(120 - 20) << std::endl;
return 0;
}This program prints:
Price: 120 EUR
With a discount: 100 EURstd::to_string(120) returns the text "120", and + attaches " EUR" to it. If you wrote just return price + " EUR";, the program would compile, but it wouldn't work: it would print nonsense or stop with an error, because C++ doesn't join a number and a text in quotes with +. And watch out for decimal numbers: std::to_string(2.5) returns "2.500000", with six decimal places.
Instructions
A pizza costs 9 and a drink costs 3. The function orderPrice should calculate the price of an order, but it only prints the price, so the program doesn't compile. Fix the function so that it returns the price instead of printing it. The program then prints Together: 63.
Below the function orderPrice, write a function formatPrice with a parameter price of type int that returns the price as text with the currency, that is, as a std::string. For 63, it returns "63 EUR". Turn the number into text with std::to_string().
Use it in the last line that main prints: instead of family + friends, print formatPrice(family + friends). The program prints Together: 63 EUR.
Start programming for free
6/10