Next: , Previous:   [Contents][Index]


5.4 Function Definitions

A command or function definition consists in a header followed by a block of instructions delimited by end.

A command definition header consists in a command name followed by a comma separated parameter declaration list enclosed in parenthesis. A parameter declaration consists in a parameter type: number, point, vector, set, line, circle, conic or string, followed by a parameter name. A function definition header is identical to a command definition header except that it starts with a return type.

A function definition must contain at least a return statement, which consists in the return keyword followed by an appropriate expression. Command definitions may contain empty return statements.

Example:

number square(number x)
  return x*x
end

Variables may be declared local to a command or function definition using the local keyword.

Example:

vector conjugate(vector v)
  local x, y
  x = abscissa(v)
  y = ordinate(v)
  return vector(x, -y)
end