5.7.1. Functions

A function takes zero or more arguments and returns an answer. Use it in an expression. For example, if you #include <cmath> then you can use the sqrt function, which takes one argument of type double and returns the (approximate) square root of that argument. Expression

  double c = 2*sqrt(49.0);
makes c = 14.0. The argument can be any expression of the correct type. For example,
  double r = 16.0;
  double y = sqrt(2*r + 4);
makes y = 6.0. The pow function takes two real numbers x and y and returns xy. For example,
  double z = pow(3.0, 4.0);
makes z approximately equal to 81.0.

Arguments or parameters

I will use words argument and parameter interchangeably. Each refers to something that is passed to a function.

Parentheses are required around arguments

Arguments to a function must be enclosed in parentheses. If there are two or more arguments, they must be separated by commas.

Functions with no arguments

Some functions take no arguments. Write an empty set of parentheses to show no arguments. For example, if function mystic takes no arguments and returns an integer, then
  int n = mystic();
runs function mystic and sets n to its result.

Use the correct number of arguments

Most functions have a fixed number of arguments. If a function takes two arguments, the use it with two arguments. If it takes one argument, then use it with one argument.

Know what a function does

Each function does a particular job. It is critical that you understand what it does. It will do you know good to hope that it does whatever you want it to do. Understand what type of value it returns, what that value means, and how the arguments affect that.

Order of evaluation

Be careful when the arguments to a function are expresions that have side-effects. The argument expressions are not necessarily computed from left to right. For example, suppose that function readInteger() reads one integer from the standard integer and returns it, and suppose that build is a function that takes two integer arguments and returns an integer. Imagine that the standard input contains
  24 92
Statement
  int r = build(readInteger(), readInteger());
can be computed in more than one way.
    1. Perform the first readInteger() call, which reads and returns 24.
    2. Perform the second readInteger() call, which reads and returns 92.
    3. Call build(24, 92).

    1. Perform the second readInteger() call, which reads and returns 24.
    2. Perform the first readInteger() call, which reads and returns 92.
    3. Call build(92, 24)

In each case, the parameters to build are the result of the first readInteger call and the result of the second readInteger call, in that order. But those results depend on when the readInteger calls are done. So you do not know what you are going to get. To avoid this problem, write
  int a = readInteger();
  int b = readInteger();
  int r = build(a,b);
so that the order of computation is explicit.


Exercises

  1. Suppose that variables a, b and c have type double, and each already has a value. Write a statement that creates variable d of type double and makes d = √b2 - 4ac. Answer

  2. If a given function takes one parameter, are you allowed to use it with two parameters? Answer

  3. Why should you avoid expressions that have side-effects as function parameters? Answer

  4. Suppose that you decide to use statement

      int r = build(readInteger(), readInteger());
    
    discussed above. You test it, and find that it does what you want it to do. Is it okay to use it then? Answer