Let's take another look at the Trace Power animation from the previous lesson. While you watch the animation, take note of the new keywords Function, Parameter, and returns. See if you can figure out the meaning of these keywords.
Now that you have had another chance to see the new keywords in the program trace, let's define their purpose more precisely. To help our discussion, the code for the Power function is listed below.
Function
Power( |
The keyword Function at the beginning of Power and the keyword EndFunction at the end of Power bound the code that comprises the subprogram. The keyword Parameter indicates a variable that is passed to the subprogram from the calling program. Parameter variables are a way of transferring information between a subprogram and its calling program. The direction of the transfer is indicated by the keywords that follow Parameter: in, or out, or in,out. Parameters specified as in are only used as input to the subprogram. Parameters specified as out are only used to return results to the calling program. Parameters specified as in,out are used for input and output. Notice in the Power function that both parameters are specified as in because their contents are only used as input to complete the power calculation. Since their values are not modified in any way to give new information to the calling program, they are not out parameters.
Our power function also demonstrates another way of transferring information to the calling program: the return keyword. This keyword is specific for subprograms that are functions. The return statement specifies a particular value that is to be returned to the calling program as the value of the function. Since the function itself is considered to have a certain value based on its input, we can write statements like the following:
answer1 :=
Call Power(2,4) |
In each of the statements above, the value returned by the call to Power is substituted into the expression. For example, the value that is assigned to answer3 is calculated as follows:
answer3
:= Call Power(3,3) + (4 * 10) / 5 |
The value 27 was substituted for the function call Power(3,3) in the expression since 33 equals 27. The final answer of 35 is obtained by evaluating the entire expression. Don't forget that the multiplication and division operations are performed before addition and subtraction.
Now let's take a look at a subprogram that is a procedure. We will use the swap operations as our example procedure. Here's the implementation of Swap.
Procedure
Swap( |
Notice that our Swap procedure is bounded by the keywords Procedure and EndProcedure. We also see that Swap has two parameters, x and y, and both of the parameters are specified as input and output parameters. They need to be input parameters since they are used as input to the function. Without two variables to exchange, we could not perform a swap operation! But they also need to be output parameters because our procedure exchanges their values, and we want are calling program to be aware of the changes that were made.
You might be wondering why we have to specify our parameters as input or output. The answer is that subprograms actually receive parameters in two different ways. The first way is called pass-by-value. When a variable is passed by value, the subprogram actually receives a copy of the value assigned to that variable. For example, consider the diagram below showing a call to the Power function:
In this example, Power receives a copy of the values stored in a and b. When the function begins executing, parameter x is equal to 2 (the value stored in a) and parameter y is equal to 5 (the value stored in y). Since Power only receives copies of these values, the contents of a and b remain unchanged after the function call.
The second way that subprograms receive parameters is called pass-by-reference. When a variable is passed by reference, the subprogram actually receives the location of the variable, not just the value. In this case, the subprogram parameter and the program variable refer to the same memory location. For example, consider the diagram below showing a call to the Swap procedure:
In this example, Swap receives the location of the variables a and b. When the procedure begins executing, parameter x is equal to the same memory location as a and parameter y is equal to the same memory location as y. Since the variables and parameters refer to the same memory location, the changes made to this location in the subprogram will also be reflected in the calling program. What has really happened is that we have given two names to the same memory location. For example, in the calling program one memory location is named a and in the subprogram the same location is named x.
As you may have guessed, in parameters are always passed by value while out and in,out parameters are passed by reference. The tables below gives a summary of parameters and their passing mechanisms.
Pass-by-value: (in) |
the parameter is assigned the value of the variable in the calling program. |
Pass-by-reference: (out and in,out) |
the parameter refers to the same memory location as the variable in the calling program. |
To help you understand how pass-by-value and pass-by-reference parameters affect the logic of a subprogram, consider what would happen if we had two versions of the swap procedure. If the one version used pass-by-reference and the other used pass-by-value, how would the results of the procedures differ? The animation below will help you answer that question.