Skip to main content

Section 5.7 More on Returning

We have already learned the basics of returning from functions. But there are a few important details we still need to cover.

Subsection 5.7.1 Returning Variables

As we have already mentioned, The type of the expression in the return statement must match the return type of the function itself. When you declare that the return type is double, you are making a promise that this function will eventually produce a double value. If you try to return with no expression, or return an expression with the wrong type, the compiler will give an error.
Although it is possible to return a literal value with something like return 42;, it is much more common to want to return the result of a computation. We can either do so by returning an expression:
double calculateArea(double radius) {
    return 3.14 * radius * radius;
}
Or by returning a local variable:
double calculateArea(double radius) {
    double area = 3.14 * radius * radius;
    return area;
}
Much like when we pass a variable to a function, when we return a variable, the value of the variable is copied as it is returned. So although we might say of the function above β€œit returns area”, it is more accurate to say β€œit returns the value stored in area” .
The two approaches will produce the same result, but the second is often easier to read and debug. Especially when you are stepping through code, first calculating an answer and storing it into a variable, then returning the value of that variable, makes it easier to see what is going on.

Note 5.7.1.

It is also worth mentioning that the two approaches will almost certainly produce the same object code when a compiler gets done optimizing the code.
Trying to β€œoptimize” code by using fewer local variables is almost always pointless. Focus on code that is easy to read and debug. The compiler can generally be counted on to turn your code into an efficient machine representation. There are times when programmers will write code in a specific way to optimize its speed, but you should not try to β€œoptimize” your use of local variables.

Checkpoint 5.7.1.

What should be the return type of the function convertToCelsius?
______ convertToCelsius(double fahrenheit) {
  double celsius;
  celsius = (fahrenheit - 32) * 5 / 9;
  return celsius;
}
  • What variable are we returning in the function, and what is the variable’s type?
  • double
  • The function returns celsius, which is a double.
  • string
  • What variable are we returning in the function, and what is the variable’s type?
  • void
  • Since we are returning something in the function, the function is not void .

Subsection 5.7.2 Void Functions

Every function must specify a return type in its prototype. But not every function needs to return a value. Some functions are used for their side effects - they do something, but they don’t produce a value. A function might print something to the screen, or play a sound or send a message over the network.
These functions have a return type of void (β€œvoid” as in nothing) and don’t need a return statement:
void printAsMoney(double dollars) {
    // round to the nearest cent
    double cents = dollars * 100;
    cents = round(cents);
    dollars = cents / 100.0;
    // then print
    cout << "$" << dollars << endl;
}
If you want to provide a return statement, you can simply return nothing when the function is done with return;
Because void functions do not return anything, there is no need to try to store their result. In fact, any attempt to use their result would be an error. If we wrote double result = printAsMoney(12.432345);, we are essentially saying double result = ; because printAsMoney(12.432345) evaluates to nothing.
Here is a full program that uses the void function:
Listing 5.7.1.
You have attempted of activities on this page.