2019-06-11 05:37:12 +04:00
---
id: functions
title: Functions
---
2019-11-08 03:19:27 +04:00
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
2019-11-08 03:19:27 +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-11-08 03:19:27 +04:00
```pascaligo
// shorthand syntax
block { skip }
// verbose syntax
begin
skip
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
Functions in PascaLIGO are defined using the `function` keyword followed by their `name` , `parameters` and `return` type definitions.
Here's how you define a basic function that accepts two `ints` and returns a single `int` :
2019-09-30 21:23:30 +04:00
```pascaligo
2019-11-08 03:19:27 +04:00
function add(const a: int; const b: int): int is
block { skip } with a + b
```
The function body consists of two parts:
- `block {<code>}` - logic of the function
- `with <value>` - the return value of the function
> 💡 `skip` can be used as a placeholder for empty function blocks, when all the neccessary logic fits into `with` at the end. It is also possible to omit `block { skip } with`
in the above example, leaving only `a + b` .
2019-06-11 05:37:12 +04:00
2019-11-08 03:19:27 +04:00
<!-- Cameligo -->
2019-06-11 05:37:12 +04:00
2019-11-08 03:19:27 +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.
Here's how you define a basic function that accepts two `ints` and returns an `int` as well:
```cameligo
let add (a: int) (b: int) : int = a + b
2019-06-11 05:37:12 +04:00
```
2019-11-08 03:19:27 +04:00
The function body is a series of expressions, which are evaluated to give the return
value.
<!-- END_DOCUSAURUS_CODE_TABS -->