![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works If you have been following the previous How-Tos, you will notice a new line of code after the using namespace std line: long multiply( long left, long right ) ; This is referred to as a function declaration. It is the functions signature. The declaration specifies the number or arguments, if any, and their data type. The declaration also shows the return type (if the function returns a value). In this declaration, multiply is the name of the function. The function accepts two arguments; both of them are of type long. The first argument has the name left and the second argument has the name right. The result of the function call, the return value, is of type long. The return value does not have a name because a function can only return a single data type. The program begins with the declaration and initialization of two long variables, value1 and value2. These two variables will be used to hold the values that will be passed to the function multiply. Next, the program displays an informational message telling the user the intent of the program. In How-To 1.3, you learned how to use a for loop. This for loop is used to continually accept new input for subsequent calculations. One difference between this for loop and the one demonstrated in the previous How-To is that the third expression is empty. for( long result = 1; result != 0; /*empty expression*/ ) This is perfectly legal; an empty expression here makes sense because you do not need to update a counting variable. This for loop will check that the value in result is not equal to 0. The first statement within the for loop initializes the variables value1, value2, and result to 0. Next, the program displays a message to the user how to quit the program. The next cout statement displays a message asking the user to enter the first of two values. The subsequent cin statement gathers input from the user and places the value into the variable named value1. Another message is displayed prompting the user to enter the second value required for the calculation. The cin statement accepts input from the user and places that value in value2. An if statement is introduced. This if statement is composed of a compound expression. The equality operator == and logical operator && are both used within the if statement. The following verbally expresses the statement: If the contents of value1 are 0 AND the contents of value2 are 0, break out of the for loop. If either expression resolves to 0, the program breaks out of the loop. Otherwise, the program continues executing to the next statement. The following statement from the program: result = multiply( value1, value2 ) ; is a call to the function named multiply. Notice that the two values gathered from the user are passed as arguments to the function. The function uses these two values to perform the multiplication. The order of evaluation for this statement is as follows: the function is called first, and then the result of the function call (the return value) is assigned to the variable named result. The next two statements after the function call display the results of the calculation. The definition for the function multiply is found beyond mains closing brace. A function definition has a body and contains program statements (a declaration does not). The two comments that precede the function name proclaim the intent of the function. As a rule, you should provide more information about a function than is provided in this example. A function definition header might look like this: // name: multiply // description: this function multiplies the two // arguments supplied and returns the result. // arguments: // long left - one of two operands for the calculation // long left - second of two operands for the calculation // return: long - the result of the multiplication The first statement within the multiply body is the declaration of a long variable named result. Notice that the name result is declared here and in the main function. The result variable declared in main is not visible to this function, so there is no name clash between the two variables. The variable result comes into scope at its declaration and continues to exist until multiplys closing brace. This statement consists of the expression left * right; the result of which is assigned to the variable result. The last line of code in the function is the return statement. The job of the return statement is to return the result of the multiplication. Comments Error checking is not included in this How-To. The intent of this How-To is to demonstrate the use of functions and function calls. Because of this, the example must be as brief as possible. It is assumed that the user will enter valid numbers for the inputs requested. Particularly, two statements in the program cin >> value1 ; //... cin >> value2 ; are used to gather input from the user. It is hoped that a user will supply a valid integer for each prompt. Unfortunately, as is usually the case, users enter data that you do not expect. Error handling will be addressed in How-To 1.7. Suffice it to say that the results will be unpredictable if you enter anything other than numeric data. The variables (actually the values) passed to the function multiply are copies of those values. This is referred to as pass-by-value; the C++ language inherits this trait from the C language. When the compiler sees the following line of code: result = multiply( value1, value2 ) ; it will create a copy of value1 and value2 to be passed on to the function multiply. The original variables passed as arguments are not operated on. The two arguments left and right are local to the function multiply. Any changes you make to those variables will not be reflected back to the original variables value1 and value2 in main. Within the function multiply, the variable result serves two purposes. It is used to hold the result of the multiplication and is used as the argument to the return statement. The compiler makes a temporary copy of result and returns it to the calling function.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|