15.2  Parameter Passing

In order to compensate for the absence of pointers and reference parameters, the parser supports two class templates of the C++ Standard Template Library: array and tuple.

The stipulation that function parameters are passed only by value dictates that native C arrays may be used only as global constants and in instances where arrays are used only locally by a function. In order to achieve the effect of passing arrays as value parameters is achieved by means of the standard array class template.

As an illustration of its use, suppose that we define a function as follows:

  array<int, 8> Sum8(array<int, 8> a, array<int, 8> b) {
    for (uint i=0; i<8; i++) {
      a[i] += b[i];
    }
    return a;
  }
If a and b are variables of type array<int, 8>, then the result of the assignment
  b = Sum8(a, b);
which does not affect the value of a, is that each entry of the array b is incremented by the corresponding entry of a.

Aside from the restriction on parameter passing, there is no semantic difference between ordinary C arrays and instances of an array template class. The pretty-printer, therefore, simply converts array objects to C arrays and prints the above definition as follows:

  int[8] Sum8(int a[8], int b[8]) {
    for (uint i=0; i<8; i++) {
      a[i] += b[i];
    }
    return a;
  }

The effect of multiple-valued functions is achieved through the tuple class template. While the same effect could be achieved by means of an ordinary struct return type, this feature provides a convenient means of simultaneously assigning the components of a returned value to local variables of the caller.

For example, the following function performs integer division and returns a quotient and remainder as a tuple: Subsection 15.1:

  tuple<uint, uint> Divide(m uint, n uint) {
    assert(n != 0);
    uint quot = 0, rem = m;
    while (rem >= n) {
      quot++;
      rem -= n;
    }
    return tuple<uint, uint>(quot, rem);
  }
A call to this function has the following syntax:
  uint q, r;
  tie(q, r) = Divide(23, 5);

The pretty-printer provides a slightly simpler syntax, printing the above definition as

  <uint, uint> Divide(m uint, n uint) {
    assert(n != 0);
    uint quot = 0, rem = m;
    while (rem >= n) {
      quot++;
      rem -= n;
    }
    return <quot, rem>;
  }
and the invocation as
  uint q, r;
  <q, r> = Divide(23, 5);

Note that our use the tuple template is intended only for the purpose of parameter-passing and is not recognized by the parser in any other context.

David Russinoff 2017-08-01