5A. Defining Functions


Defining elementary functions

One of the most important features of C++, or of almost any programming language, is your ability to define new functions. You write a large piece of software by breaking it into a collection of small functions, each with a specific purpose.

General form of a function definition

The general form of a function definition is

  R name(arguments)
  {
    body
  }
where

  • R is the type of result that the function yields,

  • name is the function's name,

  • arguments is a comma-separated list of the function's arguments. Each argument consists of a type followed by a name.

  • body is a sequence of statements that performs the actions that the function needs to do.

The part of the definition before the left brace,

  R name(arguments)
is called the function heading. Statements body are called the function body.


The return statement

In the body, statement

  return E;
indicates that the function is finished and that its result is the value of expression E. A function cannot do anything after it performs a return-statement.


Example: successor

Here is a C++ definition of function successor(n) = n + 1. Successor takes one argument of type int and yields, or returns, a result of type int.
  // Successor(n) returns n+1.
  // For example, successor(3) = 4.

  int successor(int n)
  {
    return n+1;
  }

Example: largest

Assume that we have included <algorithm> by writing
  #include <algorithm>
  using namespace std;
Then the following defines function largest so that largest(3, 6, 2) = 6.
  // largest(x, y, z) returns the largest of
  // x, y and z.

  double largest(double x, double y, double z)
  {
    return max(x, max(y, z));
  }

Each argument needs a type

Notice that each argument has a specified type. You are not allowed to write
  double largest(double x, y, z)
  {
    return max(x, max(y, z));
  }
(You might think that would be reasonable, but C++ does not allow it. Stay within the language.)


Calling your functions

You should feel free to use your functions whenever you need them. Use them exactly the way you would if they were library functions. There is really no difference between your functions and library functions.

Example: largestOfFive

For example, you can compute the largest of five numbers by doing two calls to function 'largest'.
  // largestOfFive(u, v, w, x, y) returns the largest
  // of u, v, w, x and y.

  double largestOfFive(double u, double v, double w,
                       double x, double y)
  {
    return largest(u, v, largest(w, x, y));
  }

As you can see, the parameters passed to 'largest' can be any expressions. They are not required to be variables called x, y and z, as they were in the heading of the definition of 'largest'.


The form of a function call

After seeing a type in front of each argument in a function definition, some beginners try to write types in function calls too. Don't do that. For example,

  double z = largest(a, b, c);
makes sense. But
  double z = largest(double a, double b, double c);
is not allowed in C++. Remember that you are not defining function 'largest' here, so get rid of the types in the function call.



Order of definitions matters

Functions in C++ are similar to methods in Java. But there is an important difference: Java allows you to define methods in any order, but a C++ program can only use a function after the function definition (or prototype). Order your function definitions accordingly. You can write

  int sqr(int x)
  {
    return x*x;
  }

  int fourthPower(int x)
  {
    return sqr(sqr(x));
  }
but not
  int fourthPower(int x)
  {
    return sqr(sqr(x));
  }

  int sqr(int x)
  {
    return x*x;
  }


Where do arguments come from?

Some beginners are uncomfortable with arguments. Look at this function definition.

  // sqr(x) returns the square of x.

  int sqr(int x)
  {
    return x*x;
  }
A reasonable question is, what is x? How can I multiply x times itself if I don't know what x is?

The answer is that the responsibility for choosing a value for x lies with the function call, not with the function definition. Statement

  int w = sqr(20);
chooses value 20 for x.

It is crucial that you learn to compartmentalize, to think only about a small piece of a computer program at a time. When writing a function definition, don't think about where its arguments will come from. That will be handled someplace else.


Exercises

  1. Write a definition of function cube(x), which takes a real number x (type double) and returns x3. You don't need to use pow. Answer

  2. Write a function distance(x1, y1, x2, y2), where all of the arguments are real numbers, and returns the distance between points (x1, y1) and (x2, y2) in the plane. Use the sqrt function. Answer