Functions

A function defines a mapping from an input value to an output value.

// Increment a value using the built-in increment operator +.
func a(b:@) -> @ {
  +(b)
};

a(23)

Syntactically, a function requires the following elements:

  1. func keyword

  2. Function name (all lower case)

  3. In parentheses, one or more arguments

  4. -> mapping operator

  5. Output type

  6. In braces, code block of body

  7. Terminating ;

Methods in class definitions are similar to functions, but must occur inside a class code block and do not have a prefatory func keyword.

Reentrancy/Recursion

Functions currently refer to themselves not by name but by the standard placeholder $. Thus, a Fibonacci function could look like this in Jock:

// Fibonacci series
func fib(n:@) -> @ {
  if n == 0 {
    1
  } else if n == 1 {
    1
  } else {
    $(n - 1) + $(n - 2)
  }
} 

Last updated