2019-06-11 05:37:12 +04:00
|
|
|
---
|
|
|
|
id: functions
|
|
|
|
title: Functions
|
|
|
|
---
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Writing code is fun as long as it does not get out of hand. To make
|
|
|
|
sure our code does not turn into spaghetti, we can structure some
|
|
|
|
logic into functions.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
## Blocks
|
2019-06-11 05:37:12 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
In PascaLIGO, *blocks* enable the sequential composition of
|
2020-02-06 14:47:41 +04:00
|
|
|
instructions into an isolated scope. Each block needs to include at
|
|
|
|
least one instruction. If we need a placeholder, we use the
|
|
|
|
instruction `skip` which leaves the state unchanged. The rationale
|
|
|
|
for `skip` instead of a truly empty block is that it prevents you from
|
|
|
|
writing an empty block by mistake.
|
2019-06-11 05:37:12 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
|
|
|
|
2019-12-26 15:51:42 +04:00
|
|
|
```pascaligo skip
|
2020-02-05 19:28:40 +04:00
|
|
|
// terse style
|
2020-01-28 18:13:50 +04:00
|
|
|
block { a := a + 1 }
|
2020-02-05 19:28:40 +04:00
|
|
|
// verbose style
|
2019-11-08 03:19:27 +04:00
|
|
|
begin
|
2020-01-28 18:13:50 +04:00
|
|
|
a := a + 1
|
2019-11-08 03:19:27 +04:00
|
|
|
end
|
2019-06-11 05:37:12 +04:00
|
|
|
```
|
2020-02-05 19:28:40 +04:00
|
|
|
Blocks are more versatile than simply containing instructions:
|
|
|
|
they can also include *declarations* of values, like so:
|
|
|
|
```pascaligo skip
|
|
|
|
// terse style
|
2020-02-06 14:47:41 +04:00
|
|
|
block { const a : int = 1 }
|
2020-02-05 19:28:40 +04:00
|
|
|
// verbose style
|
|
|
|
begin
|
2020-02-06 14:47:41 +04:00
|
|
|
const a : int = 1
|
2020-02-05 19:28:40 +04:00
|
|
|
end
|
|
|
|
```
|
2019-06-11 05:37:12 +04:00
|
|
|
|
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
## Defining a function
|
2019-06-11 05:37:12 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-01-28 18:13:50 +04:00
|
|
|
Functions in PascaLIGO are defined using the `function` keyword
|
|
|
|
followed by their `name`, `parameters` and `return` type definitions.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Here is how you define a basic function that computes the sum of two
|
|
|
|
integers:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2019-12-26 15:51:42 +04:00
|
|
|
```pascaligo group=a
|
2020-02-05 19:28:40 +04:00
|
|
|
function add (const a : int; const b : int) : int is
|
|
|
|
block {
|
|
|
|
const sum : int = a + b
|
|
|
|
} with sum
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
The function body consists of two parts:
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
- `block { <instructions and declarations> }` - logic of the function
|
|
|
|
- `with <value>` - the value returned by the function
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2019-11-19 04:44:36 +04:00
|
|
|
#### Blockless functions
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Functions that can contain all of their logic into a single expression
|
|
|
|
can be defined without a block. Instead of a block, you put an
|
|
|
|
expression, whose value is implicitly returned by the function, like
|
|
|
|
so:
|
2019-12-26 15:51:42 +04:00
|
|
|
```pascaligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
function add (const a: int; const b : int) : int is a + b
|
|
|
|
```
|
|
|
|
|
|
|
|
You can call the function `add` defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/functions/blockless.ligo add '(1,2)'
|
|
|
|
# Outputs: 3
|
2019-11-19 04:44:36 +04:00
|
|
|
```
|
2019-06-11 05:37:12 +04:00
|
|
|
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-06-11 05:37:12 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Functions in CameLIGO are defined using the `let` keyword, like other
|
|
|
|
values. The difference is that a succession of parameters is provided
|
|
|
|
after the value name, followed by the return type. This follows OCaml
|
|
|
|
syntax. For example:
|
|
|
|
```cameligo group=c
|
|
|
|
let add (a : int) (b : int) : int = a + b
|
|
|
|
```
|
|
|
|
|
|
|
|
You can call the function `add` defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/functions/blockless.mligo add '(1,2)'
|
|
|
|
# Outputs: 3
|
|
|
|
```
|
2020-01-28 18:13:50 +04:00
|
|
|
|
|
|
|
CameLIGO is a little different from other syntaxes when it comes to
|
|
|
|
function parameters. In OCaml, functions can only take one
|
|
|
|
parameter. To get functions with multiple arguments like we are used
|
2020-02-05 19:28:40 +04:00
|
|
|
to in imperative programming languages, a technique called
|
2020-01-28 18:13:50 +04:00
|
|
|
[currying](https://en.wikipedia.org/wiki/Currying) is used. Currying
|
|
|
|
essentially translates a function with multiple arguments into a
|
|
|
|
series of single argument functions, each returning a new function
|
|
|
|
accepting the next argument until every parameter is filled. This is
|
2020-02-05 19:28:40 +04:00
|
|
|
useful because it means that CameLIGO supports
|
2020-01-28 18:13:50 +04:00
|
|
|
[partial application](https://en.wikipedia.org/wiki/Partial_application).
|
|
|
|
|
|
|
|
Currying is however *not* the preferred way to pass function arguments
|
|
|
|
in CameLIGO. While this approach is faithful to the original OCaml,
|
2020-02-05 19:28:40 +04:00
|
|
|
it is costlier in Michelson than naive function execution accepting
|
|
|
|
multiple arguments. Instead, for most functions with more than one
|
|
|
|
parameter, we should gather the arguments in a
|
2020-01-28 18:13:50 +04:00
|
|
|
[tuple](language-basics/sets-lists-tuples.md) and pass the tuple in as
|
|
|
|
a single parameter.
|
|
|
|
|
|
|
|
Here is how you define a basic function that accepts two `ints` and
|
|
|
|
returns an `int` as well:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2019-12-26 15:51:42 +04:00
|
|
|
```cameligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
let add (a, b : int * int) : int = a + b // Uncurried
|
|
|
|
let add_curry (a : int) (b : int) : int = add (a, b) // Curried
|
|
|
|
let increment (b : int) : int = add_curry 1 // Partial application
|
|
|
|
```
|
2020-01-09 00:58:26 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `increment` function defined above using the LIGO
|
|
|
|
compiler like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/functions/curry.mligo increment 5
|
|
|
|
# Outputs: 6
|
2019-06-11 05:37:12 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The function body is a single expression, whose value is returned.
|
2019-12-11 13:34:08 +04:00
|
|
|
|
|
|
|
<!--ReasonLIGO-->
|
2020-01-28 18:13:50 +04:00
|
|
|
Functions in ReasonLIGO are defined using the `let` keyword, like
|
2020-02-05 19:28:40 +04:00
|
|
|
other values. The difference is that a succession of parameters is
|
|
|
|
provided after the value name, followed by the return type.
|
2019-12-11 13:34:08 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Here is how you define a basic function that sums two integers:
|
2019-12-26 15:51:42 +04:00
|
|
|
```reasonligo group=b
|
2020-02-05 19:28:40 +04:00
|
|
|
let add = ((a, b): (int, int)) : int => a + b;
|
2019-12-11 13:34:08 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can call the function `add` defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/functions/blockless.religo add '(1,2)'
|
|
|
|
# Outputs: 3
|
|
|
|
```
|
2019-12-11 13:34:08 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The function body is a single expression, whose value is returned.
|
2019-11-08 03:19:27 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
## 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.
|
2019-11-19 04:44:36 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Here is how to define an anonymous function:
|
2019-11-19 04:44:36 +04:00
|
|
|
|
|
|
|
<!--DOCUSAURUS_CODE_TABS-->
|
|
|
|
<!--Pascaligo-->
|
2019-12-26 15:51:42 +04:00
|
|
|
```pascaligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
function increment (const b : int) : int is
|
|
|
|
(function (const a : int) : int is a + 1) (b)
|
|
|
|
|
|
|
|
const a : int = increment (1); // a = 2
|
|
|
|
```
|
|
|
|
|
|
|
|
You can check the value of `a` defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo evaluate-value gitlab-pages/docs/language-basics/src/functions/anon.ligo a
|
|
|
|
# Outputs: 2
|
2019-11-19 04:44:36 +04:00
|
|
|
```
|
2019-11-21 17:41:22 +04:00
|
|
|
|
2019-12-12 17:35:07 +04:00
|
|
|
<!--CameLIGO-->
|
2019-12-26 15:51:42 +04:00
|
|
|
```cameligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
let increment (b : int) : int = (fun (a : int) -> a + 1) b
|
|
|
|
let a : int = increment 1 // a = 2
|
|
|
|
```
|
|
|
|
|
|
|
|
You can check the value of `a` defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo evaluate-value gitlab-pages/docs/language-basics/src/functions/anon.mligo a
|
|
|
|
# Outputs: 2
|
2019-11-21 17:41:22 +04:00
|
|
|
```
|
|
|
|
|
2019-12-11 13:34:08 +04:00
|
|
|
<!--ReasonLIGO-->
|
2019-12-26 15:51:42 +04:00
|
|
|
```reasonligo group=c
|
2020-02-05 19:28:40 +04:00
|
|
|
let increment = (b : int) : int => ((a : int) : int => a + 1)(b);
|
|
|
|
let a : int = increment (1); // a = 2
|
|
|
|
```
|
|
|
|
|
|
|
|
You can check the value of `a` defined above using the LIGO compiler
|
|
|
|
like this:
|
|
|
|
```shell
|
|
|
|
ligo evaluate-value gitlab-pages/docs/language-basics/src/functions/anon.religo a
|
|
|
|
# Outputs: 2
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
|
|
|
|
2019-11-21 17:41:22 +04:00
|
|
|
<!--END_DOCUSAURUS_CODE_TABS-->
|