9. Functions

Although functions may not seem exciting at first glance, we cannot stress enough their ultimate importance for any serious programming endeavor. They are the single most important concept of high-level programming. The next several chapters will demonstrate that more clearly, but in this chapter you will be introduced to the concept of functions in programming languages.

Problem: In an imaginary country, the price of access to the Internet is $6.99 per month plus additional $1.09 for each whole gigabyte of data transferred. What would the price be for a month with 6 gigabytes of data transferred, what for a month with 13 gigabytes transferred, and what for a month with 31 gigabytes transferred?

This task can be comprehended as an application of a simple formula: for a monthly data transfer of x whole gigabytes, the price would be 6.99 + 1.09*x dollars.

To provide an answer to the problem, we need to apply the given formula three times. One way to do it is to use the copy-paste method, which is far from being the best solution. We would rather use a method that avoids the copy-pasting and also provides many other benefits which might, at a first glance, be overlooked by a novice programmer.

Functions in C++ are somewhat similar to functions in mathematics. They have parameters that are inputs to a function, and when the values of parameters are given, the result of a function can be computed.

When the concept of a function is applied to the formula given above, the formula becomes represented by a function having a single parameter named x. The result of that function will be the value obtained by computing the formula 6.99 + 1.09*x . The result, of course, depends on the value of the parameter. A result of a function is often called a value of the function, or a function value. It is also common to say that the function returns the result.

In order to write a function in C++, we have to start with the function's header. The header for our function looks like this:

double MonthlyPrice(int x)

A header consists of several parts, the most obvious one being the function's name, which is chosen at programmer's will. We have chosen the name MonthlyPrice, since we think it describes the purpose of the function well. The name is followed by function's parameters, written inside parentheses, but each parameter must be preceded by its type. In this case, the parameter x represents the number of whole gigabytes, it is a whole number, so the type int will be suitable.

A type of the function's result must be provided in front of the function's name. As the purpose of this function is to calculate the monthly price, which is not necessarily a whole number, the result will be of type double.

On completing a function header, we have to write a function body. The function body is a statement block containing, inside obligatory braces (that is, curly brackets), any valid C++ statements. In order to write the body, you are allowed to utilize everything learned so far. The purpose of a body is to compute the function's result. The result is then designated after the keyword return. The completed function is given below:

double MonthlyPrice(int x)
{
    double price = 6.99 + 1.09*x ;
    return price;
}

Functions are, like any other part of the program, executed statement by statement. In order to execute a function, the value of function's parameters must be known. The execution of a function will explained in a few moments.

It must be emphasized that at the moment when a return statement is executed, the result becomes known, thus removing the need to continue the execution of a function even if there are some additional statements following the return statement. For that reason:

  • a return statement ends the execution of a function. The result of the function is given after the return keyword. This result then becomes the function value.

A function header and a function body are together called a function definition. The function definition also introduces a new name in the program, precisely the function's name. In our case, the name MonthlyPrice is introduced.

Let us finally make a few comments about the function body given above. The first statement of the body introduces a new variable named price, which is then assigned a value of monthly price equal to the given expression. This variable price then contains the desired result of the function and the next statement designates that by the return keyword.

To tell you the truth, we could spare[*] you some typing and write this function like this:

double MonthlyPrice(int x)
{
    return 6.99 + 1.09*x;
}

After having completed this function, what can be done with it? Well, whenever we need a program to perform the computation specified by the function, we just need to apply the function. The function is applied by using an expression of a function call, as exemplified in the code fragment:

    double priceA = MonthlyPrice(6);
    double priceB = MonthlyPrice(13);
    double priceC = MonthlyPrice(31);

    cout << "The monthly price for  6 gigabytes is $" << priceA << endl;
    cout << "The monthly price for 13 gigabytes is $" << priceB << endl;
    cout << "The monthly price for 31 gigabytes is $" << priceC << endl;

It results in:

The monthly price for  6 gigabytes is $62.8
The monthly price for 13 gigabytes is $89.4
The monthly price for 31 gigabytes is $157.8

In the first line, the expression MonthlyPrice(6) is a function call. It applies the function to produce the result. In other words, a function call is an expression. The value of that expression is equal to the function's result. To calculate the result, the function needs to be executed, thus a function call causes an execution of the function.

A function call is a compound expression[*] consisting of a function name followed by values of function's parameters (called the arguments) written inside parentheses. We will soon explain the function arguments in more detail.

Here is the entire program written more compactly[*] :

#include <iostream>
using namespace std;

double MonthlyPrice(int x)
{
    return 6.99 + 1.09*x;
}

int main() 
{     
    cout<<"The monthly price for  6 gigabytes is $"<< MonthlyPrice(6) << endl;
    cout<<"The monthly price for 13 gigabytes is $"<< MonthlyPrice(13)<< endl;
    cout<<"The monthly price for 31 gigabytes is $"<< MonthlyPrice(31)<< endl;
}

Notice that the function definition needs to be placed before the function main. Also, main is a function, too, although a bit weird one. The function main is a function that gets executed at the beginning of program's execution.

The function MonthlyPrice is called three times, once inside each cout statement. As each function call causes an execution of a function, the function MonthlyPrice will be executed three times in the given program.