Functions

Defining a Function

A function is defined by giving its return type, name, parameters and a block of statements. Parameters must be separated by commas.

int triple( int n )
{
  return n*3;
}

If the return type or any of the formal parameters are arrays, the following syntax needs to be used.

int[ ] doublearray( int[ ] d )
{
  int i;

  for i = 0 to d.length - 1 {
    d[i] = d[i] * 2;
  }

  return d;
}

Calling a Function

Once defined, the function can be called anywhere further down the code.

int a = 2;
int b = triple( a );

int[ ] t = { 1 , 2 , 3 };
t = doublearray( t );