<<

Value Returning Functions 1 Value Returning Functions The ++ provides a number of useful predefined functions. However, in many situations we may have to write our own functions. Writing a function involves two things: a declaration and a definition.

Consider the following example program. The program defines and uses a function rectangleArea which computes the area of a rectangle.

#include

using namespace std;

int rectangleArea(int, int); // function declaration // returns area of rectangle given length and width

int main() { int length, width, area; cout << "Enter numbers for length and width:"; cin >> length >> width; if ( length > 0 && width > 0 ) { area = rectangleArea(length, width); // function call cout << "Area of a rectangle with "; cout << "length = " << length << " and width = " << width; cout << " is " << area; } else cout << "One or both of your inputs is not positive"; return 0; } // end of main

// function definition int rectangleArea(int len, int wid) // function header { return (len * wid); // function body } // end of function rectangleArea

Value Returning Functions 2

Declaration of functions • A function declaration (also referred to as function ) is placed in the declaration section, before the main function.

• By this declaration, the knows that rectangleArea is a function and the function has to be supplied with two arguments and it will return a value of type int. Moreover, the first argument is of type int and the second arguments is of type int.

The general form for the function declaration has exactly four components as shown below.

return_value_type function_name (list of parameter types);

• The first component of the function declaration specifies what type of value the function returns. In our example case, the function returns a value of type int.

• The second component specifies the function name. In our example case, the function name is rectangleArea.

Note that the same rules for creating identifiers (variable names), discussed before, also apply for function names.

• The third component specifies how many, and what type of arguments that the function takes. In our example case, the function takes two arguments of type int.

• The fourth component is the semicolon ; .

Note that the parameter list in a function declaration can be specified either as

int rectangleArea(int, int); or int rectangleArea(int len, int wid); where the identifiers len and wid are simply place holders. Basically, the parameter list specifies how many and what type of arguments that the function is expected to take.

Function definition • The function definition is simply the code written for this function, so that it accomplishes the required subtask.

• The function definition is very similar to that of the main part of a program. Indeed, the main part of the program is the function definition for the function whose name is main.

Value Returning Functions 3

• A function definition consists of a function header followed by a function body.

• The function header has the same signature as the function declaration, except that the header does not have a semicolon at the end. Moreover, the function header must always list the parameter names.

Note that including the semicolon in the function header will produce a syntax error.

• The function body is enclosed within a pair of curly braces. Thus function body is just like the body of the main part of a program.

• The execution of a in the function body returns the single value computed by the function to the point in the program where the function call was made.

• A return statement consists of the keyword return followed by an expression. The function rectangleArea in our program has the following statement.

return (len * wid);

When this return statement is executed, the value of the expression len * wid is returned to the point from where the function was invoked (that is, the place at which the call originated).

• The parentheses is not necessary. We can also write the return statement as follows.

return len * wid;

• The function body can have any number of executable statements.

For example, we could write the rectangleArea function as follows.

Value Returning Functions 4

int rectangleArea(int len, int wid) { int area; area = len * wid; return area; }

Function parameters - arguments • Parameters (also called formal parameters) are variables that belong to the function and appear in the function header. In the rectangleArea function, len and wid are variables.

• Arguments appear in the function call. In the statement area = rectangleArea(length, width); the variables length and width are called the arguments of the function call.

• A function call supplies the arguments to the function. We can think of the arguments as the input data to the function.

• When a function call is made, the formal parameters of the function receive the arguments supplied by the calling statement.

• The arguments in the function call and the formal parameters in the function header are matched, one to one, in order from left to right. That is, the first parameter is assigned the value of the first argument, the second parameter is assigned the value of the second argument, and so on.

In our example program, consider the function call.

Value Returning Functions 5

A function call leads to the following sequence of actions:

1. The function call invokes the function. (That is transfers control to the function.) In our example case the function rectangleArea. 2. Before the execution begins inside the body of the function, memory (of appropriate size) for each formal parameter is allocated. 3. The argument value is copied into the matched formal parameter (the newly allocated memory). In our example case the variables len and wid would contain the same values that are in length and width respectively.

Therefore, the variables len and wid are available for rectangleArea function, as if they were declared (and initialized to appropriate values) inside the function body. Arguments in proper order We know that the formal parameters and arguments are matched by order.

• Therefore, when we are writing a call to a function, we must be careful to supply the arguments in the same order as the parameters with which they will be paired.

For example suppose that we write a function that takes two double values, divides the first value by the second, and returns the result as shown below.

// divides the first parameter by the second and returns the result double divide(double numer, double denom) { return ( numer / denom ); }

Now, suppose that we have two variables, say x = 4.0 and y = 2.0 in the calling program, and some z is supposed to get the value x / y. Then consider the following function calls.

z = divide(x, y); // This call is correct: z = x / y z = divide(y, x); // This call is incorrect: z = y / x

• The above example illustrates that if we confuse the order of the arguments in a function call, the program will not do what we want it to do. Local variables In a function we can declare variables and constants in the body of the function. As discussed before, the formal parameters in the function header are also variables that belong exclusively to the function.

Value Returning Functions 6

For example, the variables len, wid and area are the variables that belong exclusively to the function rectangleArea shown below. int rectangleArea(int len, int wid) { int area; area = len * wid; return area; }

 All these variables and constants are said to be local to the function because they are accessible only within the function.

 The memory for these local variables is created when the function call is executed and they are deleted upon completion of the function execution.

 The name of a formal parameter could be the same as or different from the name of the matched argument.

 It is important to know that the arguments are matched in order to the formal parameters; not by similarity of names. Therefore, even if an argument and its matched parameter have the same name, they are two different variables, each with its own memory cell. The of variables We know that when a variable is declared, a particular memory cell (of appropriate size) is created in the memory.

 The scope (or visibility) of a variable is the block of a program in which the variable can be accessed. That is, the statements of the program block can use that variable to refer to that particular memory cell.

 The scope of local variables (which includes formal parameters) of a function is limited to the body of the function. Argument passing by value We have been referring to the parameters in the function header as formal parameters.

• Parameters in a function header are formal in the sense that any name can be used for them without changing the meaning of the function. In mathematics, we have the same situation; for example, f(x) = x2 and f(y) = y2 define the same function.

• Formal parameters are local to a function.

Value Returning Functions 7

• When a function call is made, a copy of the value of the actual argument is passed to the corresponding formal parameter. That is, the arguments are passed by value.

• With pass by value, a function can work only on copies of arguments, not on the actual arguments themselves. Thus, the actual arguments have the same value before and after the function call.

For example, consider the following program:

#include

using namespace std;

int changeValue(int x); // function declaration

// changes x value as x = x+10 and prints the new value of x int main() { int x = 40; cout << "Value of x in main before function call = " << x << endl; int y = changeValue(x); cout << "Value of x in main after return from function = " << x << endl;

return 0; } // end function main

// function definition int changeValue(int x) // function header { x += 10; cout << "Value of x after x = x+10 in the function = " << x << endl; return x; }

Output of the above program.

Value of x in main before function call = 40

Value Returning Functions 8

Value of x after x = x+10 in the function = 50 Value of x in main after return from function = 40 Can we change the value of the argument inside a function? • Yes. We can modify the argument supplied to the calling function by using a different method of passing arguments. • One way to do this is called passing argument by reference. • Pass by reference is a way to pass arguments without copying values. C++ supports reference parameters for functions. We discuss this later.