Prev Index Next

Functions

Functions in C* have a new syntax:

function main() => int {
...
}

This syntax is both more readable and easier to compile than the C++ syntax. The => int clause specifies the return type of the function. To specify void functions (also called procedures), just leave off that clause:

function f();

Many operators in C* are user-definable. To define an operator, use the same syntax as for functions, except replace "function" with "operator":

operator *(int n, char c) {
...
}

This defines the multiplication operator for an int and a char (perhaps to return a string containing n copies of c). To call it, you would use the * operator with the appropriate argument types:

3 * 'c';

Overloading of functions is allowed in C* as well. The overloading algorithm to determine which function is called for a given call site is as follows:

  1. First find the set of all applicable functions: the functions of the appropriate name for which there is a conversion from the argument types to the parameter types. Call this set A.
  2. Look for a most specific function in A; a function whose parameters are subtypes of all the other functions in A. If there is such a function, call it; otherwise, give an ambiguity error.


Prev Index Next