1.5 Functions
Functions are important building blocks of C++ programs. The first example we have seen is the main function in the hello-world program. We will say a little more about main in Section 1.5.5.
1.5.1 Arguments
C++ distinguishes two forms of passing arguments: by value and by reference.
1.5.1.1 Call by Value
When we pass an argument to a function, it creates a copy by default. For instance, the following function increments x but not visibly to the outside world:
void increment (int x) { x++; } int main() { int i= 4; increment(i); // Does not increment i cout ≪ "i is " ≪ i ≪ '\n'; }
The output is 4. The operation x++ only increments a local copy of i within the increment function but not i itself. This kind of argument transfer is referred to as Call by Value or Pass by Value.
1.5.1.2 Call by Reference
To modify function parameters, we have to Pass the argument by Reference:
void increment (int& x) { x++; }
Now, the variable itself is incremented and the output will be 5 as expected. We will discuss references in more detail in §1.8.4.
Temporary variables—like the result of an operation—cannot be passed by reference:
increment (i + 9); // Error : temporary not referable
In order to pass the expression to this function, we must store it to a variable up front and pass that variable. Obviously, modifying functions on temporary variables makes no sense anyway since we never see the impact of the modification.
Larger data structures like vectors and matrices are almost always passed by reference to avoid expensive copy operations:
double two_norm(vector& v) { ... }
An operation like a norm should not change its argument. But passing the vector by reference bears the risk of accidentally overwriting it. To make sure our vector is not changed (and not copied either), we pass it as a constant reference:
double two_norm(const vector& v) { ... }
If we tried to change v in this function the compiler would emit an error.
Both call-by-value and constant references ascertain that the argument is not altered but by different means:
Arguments that are passed by value can be changed in the function since the function works with a copy.9
With const references we work directly on the passed argument, but all operations that might change the argument are forbidden. In particular, const-reference arguments cannot appear on the left-hand side (LHS) of an assignment or be passed as non-const references to other functions; in fact, the LHS argument of an assignment is also a non-const reference.
In contrast to mutable10 references, constant ones allow for passing temporaries:
alpha= two_norm(v + w);
This is admittedly not entirely consequential on the language design side, but it makes the life of programmers much easier.
1.5.1.3 Default Arguments
If an argument usually has the same value, we can declare it with a default value. Say we implement a function that computes the n-th root and mostly the square root, then we can write:
double root(double x, int degree= 2) { ... }
This function can be called with one or two arguments:
x= root(3.5, 3); y= root(7.0); // like root(7.0, 2)
We can declare multiple defaults but only at the end of the parameter list. In other words, after an argument with a default value we cannot have one without.
Default values are also helpful when extra parameters are added. Let us assume that we have a function that draws circles:
draw_circle(int x, int y, float radius);
These circles are all black. Later, we add a color:
draw_circle(int x, int y, float radius, color c= black);
Thanks to the default argument, we do not need to refactor our application since the calls of draw_circle with three arguments still work.
1.5.2 Returning Results
In the earlier examples, we only returned double or int. These are well-behaved return types. Now we will look at the extremes: large or no data.
1.5.2.1 Returning Large Data Structures
Functions that compute new values of large data structures are more difficult. For the details, we will put you off until later and only mention the options here. The good news is that compilers are smart enough to elide the copy of the return value in many cases; see Section 2.3.5.3. In addition, the move semantics (Section 2.3.5) where data of temporaries is stolen avoids copies when the aforementioned elision does not apply. Advanced libraries avoid returning large data structures altogether with a technique called expression templates and delay the computation until it is known where to store the result (Section 5.3.2). In any case, we must not return references to local function variables (Section 1.8.6).
1.5.2.2 Returning Nothing
Syntactically, each function must return something even if there is nothing to return. This dilemma is solved by the void type named void. For instance, a function that just prints x has no result:
void print_x(int x) { std::cout ≪ "The value x is " ≪ x ≪ '\n'; }
void is not a real type but more of a placeholder that enables us to omit returning a value. We cannot define void objects:
void nothing; // Error: no void objects
A void function can be terminated earlier:
void heavy_compute(const vector& x, double eps, vector& y) { for (...) { ... if (two_norm(y) < eps) return; } }
with a no-argument return. Returning something in a void function would be an error. The only thing that can appear in its return statement is the call of another void function (as a shortcut of the call plus an empty return).
1.5.3 Inlining
Calling a function is relatively expensive: registers must be stored, arguments copied on the stack, and so on. To avoid this overhead, the compiler can inline function calls. In this case, the function call is substituted with the operations contained in the function. The programmer can ask the compiler to do so with the appropriate keyword:
inline double square(double x) { return x*x; }
However, the compiler is not obliged to inline. Conversely, it can inline functions without the keyword if this seems promising for performance. The inline declaration still has its use: for including a function in multiple compile units, which we will discuss in Section 7.2.3.2.
1.5.4 Overloading
In C++, functions can share the same name as long as their parameter declarations are sufficiently different. This is called Function Overloading. Let us first look at an example:
#include <iostream> #include <cmath> int divide(int a, int b) { return a / b ; } float divide(float a, float b) { return std::floor( a / b ) ; } int main() { int x= 5, y= 2; float n= 5.0 , m= 2.0; std::cout ≪ divide (x, y) ≪ std::endl; std::cout ≪ divide (n, m) ≪ std::endl; std::cout ≪ divide (x, m) ≪ std::endl; // Error : ambiguous }
Here we defined the function divide twice: with int and float parameters. When we call divide, the compiler performs an Overload Resolution:
Is there an overload that matches the argument type(s) exactly? Take it; otherwise:
Are there overloads that match after conversion? How many?
0: Error: No matching function found.
1: Take it.
> 1: Error: ambiguous call.
How does this apply to our example? The calls divide(x, y) and divide(n, m) are exact matches. For divide(x, m), no overload matches exactly and both by Implicit Conversion so that it’s ambiguous.
The term implicit conversion requires some explanation. We have already seen that the language’s numeric types can be converted one to another. These are implicit conversions as demonstrated in the example. When we later define our own types, we can implement a conversion from another type to it or conversely from our new type to an existing one.
⇒ c++11/overload_testing.cpp
More formally phrased, function overloads must differ in their Signature. In C++, the signature consists of
The function name;
The number of arguments, called Arity; and
The types of the arguments (in their respective order).
In contrast, overloads varying only in the return type or the argument names have the same signature and are considered as (forbidden) redefinitions:
void f(int x) {} void f(int y) {} // Redefinition: only argument name different long f(int x) {} // Redefinition: only return type different
That functions with different names or arity are distinct goes without saying. The presence of a reference symbol turns the argument type into another argument type (thus, f(int) and f(int&) can coexist). The following three overloads have different signatures:
void f(int x) {} // #1 void f(int& x) {} // #2 void f(const int& x) {} // #3
This code snippet compiles. Problems will arise, however, when we call f:
int i= 3; const int ci= 4; f(i); f(ci); f(3);
All three function calls are ambiguous: for the first call #1 and #2 are equal matches, and for the other calls #1 and #3. Mixing overloads of reference and value arguments almost always fails. Thus, when one overload has a reference qualified argument, the corresponding argument of the other overloads should be reference qualified as well. We can achieve this in our toy example by omitting the value-argument overload. Then f(3) and f(ci) will resolve to the overload with the constant reference and f(i) to that with the mutable one.
1.5.5 main Function
The main function is not fundamentally different from any other function. There are two signatures allowed in the standard:
int main()
or:
int main(int argc, char* argv[])
The latter is equivalent to:
int main(int argc, char** argv)
The parameter argv contains the list of arguments and argc its length. The first argument (argv[0]) is on most systems the name of the called executable (which may be different from the source code name). To play with the arguments, we can write a short program called argc_argv_test:
int main (int argc, char* argv[]) { for (int i= 0; i < argc; ++i) cout ≪ argv[i] ≪ '\n'; return 0; }
Calling this program with the following options:
argc_argv_test first second third fourth
yields:
argc_argv_test first second third fourth
As you can see, each space in the command splits the arguments. The main function returns an integer as exit code that states whether the program finished correctly or not. Returning 0 (or the macro EXIT_SUCCESS from <cstdlib>) represents success and every other value a failure. It is standards compliant to omit the return statement in the main function. In this case, return 0; is automatically inserted. Some extra details are found in Section A.2.4.