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:
func
keywordFunction name (all lower case)
In parentheses, one or more arguments
->
mapping operatorOutput type
In braces, code block of body
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