24B. Logical Parameter Passing Modes

Call by value and call by reference are mechanisms for passing parameters. But you often prefer to think in terms in a more conceptual viewpoint. There are three logical calling modes.

In-parameters

An in-parameter is information being passed from the caller to the function. For small things, such as integers or real numbers, you usually use call by value for an in-parameter. For example, if you think about computing sqrt(16.0), the caller tells sqrt which number (16.0) it wants the square root of.

Larger pieces of information are usually passed by pointer or by reference, even when they are in-parameters. To indicate that the parameter is an in-parameter, write const in front of the parameter in the function definition heading. For example, the following function only wants to look at the values in an array.

  // sum(A,n) yields the sum A[0] + A[1] + ... + A[n-1]

  int sum(const int* A, int n)
  {
    int result = 0;
    for(int i = 0; i < n; i++)
    {
      result += A[i];
    }
    return result;
  }

Out-parameters

An out-parameter represents information that is passed from the function back to its caller. The function accomplishes that by storing a value into that parameter. Use call by reference or call by pointer for an out-parameter. For example, the following function has two in-parameters and two out-parameters.

  // divide(x,y,q,r):
  //   x, y: in-parameters,
  //   q, r: out-parameters.
  //
  // Divide sets
  //   q = the integer quotient when you divide x by y.
  //   r = the integer remainder when you divide x by y.
  //
  // This function uses the standard mathematical definition
  // of integer division, which is a little different from
  // the C++ definition.  It requires that
  //
  //   y > 0
  //
  // and ensures that
  //
  //   0 <= r < y
  //   q*y + r = x

  void divide(int x, int y, int& q, int& r)
  {
    if(x >= 0)
    {
      q = x/y;
      r = x % y;
    }
    else
    {
      r = y - ((-x) % y);
      q = (x - r)/y;
    }
  }

When an array is an out-parameter, where you want to store information into the array, pass it by pointer, not by reference. The array is already a pointer, which gives the function read/write access to the array's chunk.

If you want to allocate a chunk and store the address of that chunk into a parameter, pass the array and by reference. For example, a function with heading

  int makeArray(int n, double*& A)
can store the address of a new chunk into A.

In-out-parameters

An in-out-parameter is used both to pass information to the function and to get new information from the function. For example, in function incr in the preceding page, n is an in-out parameter since the function body (1) looks at the value of n before storing anything into n, and (2) stores a new value into n. Use call by reference or call by pointer for an in-out parameter.

Exercises

  1. Can you use call-by-value for an out-parameter? Answer

  2. Are all in-parameters passed using call by value? Answer