Fermat
Search
K

Functions

You can create a function by assigning a code block to a variable.
set [ID] to (p1,p2, ... ,pN):
[body]
end
So let's create a function that always prints "Hello World!":
set printHello to ():
print "Hello World!"
end
To execute any user defined function we have to make a do call. Taking previous example:
do printHello
Output:
> "Hello World!"
If we want to add arguments to our functions, we just have to add the arguments between the parenthesis. For example, we can create the sum function that sums two arguments:
set sum to (x,y):
print x+y
end
do sum with 1 and 2
Output:
> 3
For single argument procedures you have an implicit argument it to use within the body of the function:
set plusThree to :
set four to it + 3
print four
end
do plusThree with 1
Output:
> 4
We also support single line procedures that doesn't request the end syntax at the end. The previous program can be simplified to:
set sum to (x,y): print x + y
do sum with 1 and 2
Output:
> 3
Alternatively to the : notation, we can be replaced it with an arrow -> syntax for single line procedures.
set [ID] to (p1,p2, ... ,pN) ->
[body]