4.6. The return keyword¶
The return keyword immediately returns from a function. If the function returns a value, then an expression or value may follow the return keyword.
The first example is area, which takes a double as a parameter, and returns the area of a circle with the given radius:
double area (double radius) {
const double pi = 4*atan (1.0);
double area = pi * radius * radius;
return area;
}
The first thing you should notice is that the beginning of the function
definition is different. Instead of void, which indicates a void
function, we see double, which indicates that the return value from this
function will have type double.
Void functions do not need a return statement, but all functions that declare a return value must have at least one return statement, or the program will crash when the function is called.
Note
Functions can have any return type in C++. The return type is always specified before the name of the function.
Also, notice that the last line is an alternate form of the return statement that includes a return value. This statement means, “return immediately from this function and use the following expression as a return value.” The expression you provide can be arbitrarily complicated, so we could have written this function more concisely:
double area (double radius) {
return 4*atan(1.0) * radius * radius;
}
On the other hand, temporary variables like area often make
debugging easier. In either case, the type of the expression in the
return statement must match the return type of the function. In other
words, when you declare that the return type is double, you are making a
promise that this function will eventually produce a double. If you try
to return with no expression, or an expression with the wrong type, the
compiler will take you to task.
Q1
What should the return type of the below function be?
________ weather (int temp) {
string outside = "";
if (temp < 50) {
outside = "cold";
}
else {
outside = "warm"
}
return outside;
}
Q2
What will print?
#include <iostream>
using namespace std;
int times_two(int x) {
int y = x;
return y;
y = y * 2;
}
int main () {
int i = 2;
cout << times_two(i);
return 0;
}
Q3
A variable that exists only inside a function, is called a variable.
More to Explore
The return keyword from cppreference.com