ligo/gitlab-pages/docs/language-basics/functions.md

143 lines
4.2 KiB
Markdown
Raw Normal View History

2019-06-11 05:37:12 +04:00
---
id: functions
title: Functions
---
Writing code is fun as long as it doesn't get out of hand. To make sure our code doesn't turn into spaghetti we can group some logic into functions.
## Instruction blocks
2019-06-11 05:37:12 +04:00
With `block`(s) you can wrap *instructions* and *expressions* into an isolated scope.
Each `block` needs to include at least one `instruction`, or a *placeholder* instruction called `skip`.
2019-06-11 05:37:12 +04:00
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 15:51:42 +04:00
```pascaligo skip
// shorthand syntax
2020-01-28 18:13:50 +04:00
block { a := a + 1 }
// verbose syntax
begin
2020-01-28 18:13:50 +04:00
a := a + 1
end
2019-06-11 05:37:12 +04:00
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Defining a function
2019-06-11 05:37:12 +04:00
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
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.
2020-01-28 18:13:50 +04:00
Here's how you define a basic function that accepts two `int`s and
returns a single `int`:
2019-12-26 15:51:42 +04:00
```pascaligo group=a
2020-01-28 18:13:50 +04:00
function add(const a: int; const b: int): int is
begin
const result: int = a + b;
end with result;
```
The function body consists of two parts:
- `block {<code>}` - logic of the function
- `with <value>` - the return value of the function
#### Blockless functions
2020-01-28 18:13:50 +04:00
Functions that can contain all of their logic into a single
instruction/expression, can be defined without the surrounding
`block`. Instead, you can inline the necessary logic directly, like
this:
2019-12-26 15:51:42 +04:00
```pascaligo group=b
function add(const a: int; const b: int): int is a + b
```
2019-06-11 05:37:12 +04:00
<!--CameLIGO-->
2019-06-11 05:37:12 +04:00
2020-01-28 18:13:50 +04:00
Functions in CameLIGO are defined using the `let` keyword, like value
bindings. The difference is that after the value name a list of
function parameters is provided, along with a return type.
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
to in traditional programming languages, a technique called
[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
useful because it means that CameLIGO can support
[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,
it's costlier in Michelson than naive function execution accepting
multiple arguments. Instead for most functions with more than one
parameter we should place the arguments in a
[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-12-26 15:51:42 +04:00
```cameligo group=b
let add (a,b: int * int) : int = a + b
let add_curry (a: int) (b: int) : int = a + b
2019-06-11 05:37:12 +04:00
```
2020-01-28 18:13:50 +04:00
The function body is a series of expressions, which are evaluated to
give the return value.
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
value bindings. The difference is that after the value name a list of
function parameters is provided, along with a return type.
2019-12-11 13:34:08 +04:00
2020-01-28 18:13:50 +04:00
Here is how you define a basic function that accepts two `int`s and
returns an `int` as well:
2019-12-11 13:34:08 +04:00
2019-12-26 15:51:42 +04:00
```reasonligo group=b
let add = ((a,b): (int, int)) : int => a + b;
2019-12-11 13:34:08 +04:00
```
2020-01-28 18:13:50 +04:00
The function body is a series of expressions, which are evaluated to
give the return value.
2019-12-11 13:34:08 +04:00
<!--END_DOCUSAURUS_CODE_TABS-->
## Anonymous functions
2020-01-28 18:13:50 +04:00
Functions without a name, also known as anonymous functions are useful
in cases when you want to pass the function as an argument or assign
it to a key in a record or a map.
2020-01-28 18:13:50 +04:00
Here's how to define an anonymous function assigned to a variable
`increment`, with it is appropriate function type signature.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
2019-12-26 15:51:42 +04:00
```pascaligo group=c
2020-01-28 18:13:50 +04:00
const increment : int -> int = function (const i : int) : int is i + 1;
// a = 2
2020-01-28 18:13:50 +04:00
const a: int = increment (1);
```
<!--CameLIGO-->
2019-12-26 15:51:42 +04:00
```cameligo group=c
2020-01-28 18:13:50 +04:00
let increment : int -> int = fun (i: int) -> i + 1
```
2019-12-11 13:34:08 +04:00
<!--ReasonLIGO-->
2019-12-26 15:51:42 +04:00
```reasonligo group=c
2019-12-10 17:47:31 +04:00
let increment: (int => int) = (i: int) => i + 1;
```
<!--END_DOCUSAURUS_CODE_TABS-->