Programming Example 8: Passing Parameters by Value and by Reference

Introduction

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:

  1. 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.
  2. 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.
  3. 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 here.

The example program is located here.  An executable of this program can be found here.