The Hyper Programming Language

Parameter Passing


All parameters are passed by reference. This means that when you pass the value of an expression as a parameter to a procedure or a constructor, the value is not copied. The callee gets the address of the value. This is the same as using a reference for a parameter in C++.

Contrary to what you might think, this convention can not be a source of bugs. The value of a parameter can not be accidentally modified from inside a procedure, because parameters are by default input parameters, and those are read-only.

Input parameters

An input parameter is the default for a parameter that does not specify otherwise. It is not marked by a keyword (but such an optional keyword could still be added in the future).

This kind of parameter is used for passing a value without allowing the callee to modify any aspect of it. This means that the entire type is automatically marked as const. An example:

procedure abc(x : int, y : [] int, z : * [] * int)
  var s : int = x   # OK
  x = 7  # ERROR : x is const
  y[3] = s  # ERROR : the array y is const
  z =$ new [100] * int  # ERROR : the pointer z is const
  z[0] =$ new int(10)  # ERROR : the array z is const
  z[2] = 99  # ERROR : the int that is pointed to by z[2] is const

To avoid confusion with input/output parameters, it is actually forbidden to use the const keyword in the type of an input parameter:

procedure p(d : const int)  # ERROR : const not allowed for an input parameter
  # (...)

Input/Output parameters

An input/output parameter allows the callee to modify the values it receives. The keyword inout is used to mark such a parameter. Any aspect that is not to be modified must be explicitly marked as const. Example:

procedure abc(inout x : intinout y : [] intinout z : const * [] * int)
  var s : int = x   # OK
  x = 7  # OK
  y[3] = s  # OK
  z =$ new [100] * int  # ERROR : the pointer z is const
  z[0] =$ new int(10)  # OK
  z[2] = 99  # OK

See also

Valid HTML 4.01 Strict Valid CSS!