Lecture 4: Using Functions

We have seen, so far, simple C++ programs that can accomplish certain computations. But for more complex programs we would like to "educate" the compiler so that it knows, in addition to the very limited basic C++ instructions, other formulas that may help to solve certain problems.

For example, if we are to write a program dealing with two-dimensional geometric object, we might want to teach the compiler the formulas to find:

the area of a square, given the side length x
the area of a rectangle, given the width and height
the area of a right triangle, given the base and height
the area of a circle, given its radius

C++ offers a convenient mechanism to define new functions, similarly to mathematical functions.

Example: Suppose we want to teach the compiler the formula for computing the area of a square, given the sidelength x. Mathematically, the formula, of course, would be:

   A(x) = x2

In other words, the formula takes as input a number (double) x, and returns to us another number that represents the area of a square.

To model the same function in C++, we first need to know the syntax for defining a function in C++.

Defining a Function

To define a function in C++, the syntax is as follows:

   type FunctionName(input)
      // some stuff
      return expression;


type is the type that the function should return (double, int, or char)
FunctionName is the name of the function (letters only, no spaces, case-sensitive as always)
input is a single variable togehter with its type, or a list of variables with their type, seperated by comma
the expression after the return statement is the answer that the function should return. It must be of the same type that the function has been defined to return

Example: Define a C++ function that computes the area of a square, given its side length x.

First, we decide on a name: we will call the function "AreaOfSquare". Next, the input variable is of type double, and we will not call it x as in math, but rather side, since variable names should always indicate their meaning. Finally, the function should return a double number (representing the area). Therefore, or function header would be:

   double AreaOfSquare(double side)
      // whatever

Next, we need to decide on what exactly the function has to do. In our case, it needs to compute the area, i.e. the square of the side. Since C++ has no built-in power function, we define the complete function as follows:

   double AreaOfSquare(double side)
      return side * side;

This function can now be used just as a mathematical function. Here is a complete program that asks the user to input the side length of a square and returns the area of that square.

   #include <iostream.h>
   double AreaOfSquare(double side)
      return side * side;

   int main(void)
      double number;
      cout << "Enter side length of square: "; cin>> number;
      cout << "Area of square is: " << AreaOfSquare(number) << "\n"; return 0; } 
In other words, the new function is defined at the top of our program, right after the <iostream.h>. Then, the standard int main(void) block starts. Note that we are using the function to compute the answer, and we directly display that answer on the screen as a double number.

Note: Now the statement int main(void) that is part of virtually every C++ program also makes sense: we are defining a function named main that returns the type int - hence the return 0 statement at the end of that function. Finally, the main function takes as input something called void which we have not yet covered.

Now let's start working on a longer example. Suppose we want to create a program that can convert the various length scales that are commonly used into other scales. For example, we want to convert inches to centimeter, or feet, or meter, or kilometer, and kilometer to inches, centimeter, feet, and so forth. There are a lot of possibilities converting the scales centimeter, inches, feet, meter, and kilometer into one another, so we have to be somewhat smart in order to create a reasonably short program.

Let's start by defining functions that convert at least one scale into another one, and later think about converting a scale into every possible other scale.

Let's start with converting inches to feet and back. Mathematically, the number of inches per feet is 12, so that the appropriate formulas would be:

f(x) = 12*x converts x from feet to inches
g(x) = x / 12 converts x from inches to feet
In C++, we of course do not use x, f, g for variable and function
names. Instead, we would define both functions as follows:

   double FeetToInches(double feet)
      return 12.0 * feet;
   double InchesToFeet(double inches)
      return inches / 12.0;
because both functions take a double as input and deliver a double as
output via the return statement. Note that we used
12.0 instead of just 12 because everything should
represent a double type in C++.

Here are the mathematical functions to convert feet to meters and back:

f(x) = 3.28 * x would convert meters x into feet
g(x) = x / 3.28 would convert feet x into meters
It is left as an exercise to define the corresponding functions using the
appropriate C++ syntax.

As our final example, C++ functions - just as in math - can have more than one input variables (but, again just as in math, they can have only one output variable).

Example: Define a C++ function that returns the area of a rectangle with a given width and height.

Mathematically, the formula would be A(h,w) = h * w, where h is the height and w is the width of the rectangle. Translating this into our C++ lingo, and using the appropriate variable and function names, we would define the function as follows:

   double AreaOfRectangle(double width, double height)
      return width * height;