Information Technology Reference
In-Depth Information
Value Parameters
There are several kinds of parameters, which pass data to and from the method in slightly dif-
ferent ways. The kind you have been looking at so far is the default type and is called a
value
parameter
.
When you use value parameters, data is passed to the method by copying the value of
the actual parameter to the formal parameter. When a method is called, the system does the
following:
Allocates space on the stack for the formal parameter
Copies the actual parameter to the formal parameter
An actual parameter for a value parameter does not have to be a variable. It can be any
expression evaluating to the matching data type. For example, the following code shows two
method calls. In the first, the actual parameter is a variable of type
float
. In the second, it is an
expression that evaluates to
float
.
float func1( float Val ) // Declare the method.
{ ... }
↑
Float data type
{
float j = 2.6F;
float k = 5.1F;
float variable
↓
float fValue1 = func1( k ); // Method call
float fValue2 = func1(
(k + j) / 3
); // Method call
...
↑
Expression that evaluates to a float
Variables must be assigned to, before being used as actual parameters (except in the case
of output parameters, which I will cover shortly). For reference types, the variable can be
assigned either a reference or
null
.
Chapter 3 covered
value types
, which, as you will remember, are types that contain their
own data. Don't be confused that I am now talking about
value parameters
. They are entirely
different. Remember that
value parameters
are parameters where the value of the actual
parameter is copied to the formal parameter.