Programming Example 8: Passing Parameters by Value and by Reference
As you read through this example, look for code that illustrates these important principles
that relate to passing parameters by value and passing parameters by reference:
- Passing By Value: A parameter is passed by value when just its data type is mentioned
in the method header. When passed by value, a copy of the parameter's value is
passed to the method.
- Passing by Reference: A parameter is passed by reference when the keyword ref precedes the data type
in the method header. When a parameter is passed by reference a copy is not made. Rather, a
reference to the variable is passed to the method.
- Side Effect: When a variable is passed by reference, it is possible for the method
to alter the value in that variable. This is called a side effect. In general, we do not
want methods to have side effects, but sometimes side effects are useful.
The problem statement for this program is located
The example program is located
here. An executable of this program can be found