Home    Reference > C Tags > @x:func
prev next

Description

This command declares a function.

Command Syntax

'@x:func' <id> {'[' <func formals> ']'}? {'(' <func formals> ')'}? {'~' <exp>}?
<func formals> ::=
<func formals> ::= '%' <id>
<func formals> ::= {<func formal> ','}* <func formal>
<func formal> ::= <id> {'~' <exp>}?

The following options are permitted.

lhs This function is a left-hand-side function that can be called from the left-hand-side of assignments. There must also be a rhs function with the same name declared in the same scope. Only one of lhs and rhs can be specified on a single function.
rhs This function is a left-hand-side function that can be called from the left-hand-side of assignments. There must also be a rhs function with the same name declared in the same scope. Only one of lhs and rhs can be specified on a single function.
visible The function name is declared as visible in the enclosing scope. If visible is specified, then local can not be specified. For lhs-rhs function pairs either both or neither must be visible.
local The function name is declared as local in the enclosing scope. If local is specified, then visible can not be specified. For lhs-rhs function pairs either both or neither must be local. The local option is only used for names declared directly within @x:type, @x:view and @x:view commands.

Name Semantics

The func name is declared the scope that surrounds the @x:func command. The func formal <id>s are declared within the @x:func command scope.

Execution Semantics

Execution of the @x:func command associates the function value with the instance of the function name. The result of execution of the @x:func command is null. The body of the function is executed only when the function is called. A function can no longer be called after the scope containing the function command exits.

Examples


   @x:func fact(n) {
      @x:if n == 0 {
         1
      } @x:else {
         n * fact(n - 1)
      }
   }
   @x:for i := 1..10 {
      @x:const j := fact(i);
      "fact(&i;)=&j;&eol;"
   }


   @x:func sum[t](a~t,b~t)~t {
      a + b
   }
   sum[int](2,3);
   sum[float](2.3,7.99)

See curry example.