Lecture 4: Using FunctionsWe 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 etc. 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) = x^{2}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 FunctionTo define a function in C++, the syntax is as follows:

type FunctionName(input) { // some stuff return expression; }where

typeis the type that the function should return (double, int, or char)FunctionNameis the name of the function (letters only, no spaces, case-sensitive as always)inputis a single variable togehter with its type, or a list of variables with their type, seperated by commathe expression after the returnstatement 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 lengthx.First, we decide on a name: we will call the function "AreaOfSquare". Next, the input variable is of type double, and we will

notcall itxas in math, but ratherside, 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 standardint 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 statementint main(void)that is part of virtually every C++ program also makes sense: we are defining a function namedmainthat returns the typeint- hence thereturn 0statement at the end of that function. Finally, themainfunction takes as input something calledvoidwhich 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 possibleother 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*xconverts x from feet to inchesg(x) = x / 12converts x from inches to feetIn C++, we of course do not usex, f, gfor 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 thereturnstatement.Notethat we used12.0instead of just12because everything should represent a double type in C++.Here are the mathematical functions to convert feet to meters and back:

f(x) = 3.28 * xwould convert metersxinto feetg(x) = x / 3.28would convert feetxinto metersIt 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, wherehis the height andwis 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; }(bgw)