When to Use Call-by-Value and Call-by-Reference?

The following guidelines will help you decide the choice between using call-by-value and call-by-reference arguments in your functions.

  1. If an argument is needed to pass a value into a function and the value of its corresponding formal parameter is not returned by the function to the calling function, it is called an input argument. An input argument should be a value argument. This protects the actual argument's value from getting altered by the function. For example, see the "show_results" function.

  2. If an argument has no value when the function is called, but its corresponding formal argument acquires a value when the function is executed, and that value must be returned to the calling function, then that argument is called an output argument. An output argument should be a reference argument. For example, see the "get_numbers" function. Alternatively that value can also be returned as the value of the function.

  3. If an argument passes a value to the function when the function is called, and that value is modified within the function and is returned to the calling function, it is called an input/output (or inout) argument. An inout argument should be a reference argument. For example, see the "swap_values" function.