LIGO functions are the basic building block of contracts. Each entrypoint of a contract is a function and each smart contract must have at least one function named main that dispatches the control flow to other functions.
When calling a function, LIGO makes a copy of the arguments but also the environment variables. Therefore any modification to these will not be reflected outside the scope of the function and will be lost if not explicitly returned by the function.
There are 2 types of functions in ReasonLIGO, Block Functions and Blockless Functions :
Functions in ReasonLIGO are defined using the let keyword, like other values. The difference is that a tuple of parameters is provided after the value name, with its type, then followed by the return type.
Functions in ReasonLIGO are defined using the following syntax :
As in CameLIGO and with blockless functions in PascaLIGO, the function body is a single expression, whose value is returned.
If the body contains more than a single expression, you use block between braces:
Anonymous functions (a.k.a. lambdas)
It is possible to define functions without assigning them a name. They are useful when you want to pass them as arguments, or assign them to a key in a record or a map.
If the example above seems contrived, here is a more common design pattern for lambdas: to be used as parameters to functions. Consider the use case of having a list of integers and mapping the increment function to all its elements.
It’s possible to place functions inside other functions. These functions have access to variables in the same scope.
LIGO functions are not recursive by default, the user need to indicate that the function is recursive.
At the moment, recursive function are limited to one (possibly tupled) parameter and recursion is limited to tail recursion (i.e the recursive call should be the last expression of the function) In ReasonLigo recursive functions are defined using the
1- Write an block function modify_ship taking as argument my_ship of type ship_code and returning a varible of type ship_code as well.
2- In the block, copy/cut the code from the previous chapter that modified the third attribute from 0 to 1 and assign the result to a constant modified_ship
3- Return modified_ship