2019-11-08 03:19:27 +04:00
|
|
|
---
|
|
|
|
id: constants-and-variables
|
|
|
|
title: Constants & Variables
|
|
|
|
---
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
import Syntax from '@theme/Syntax';
|
|
|
|
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
The next building block after types are *constants* and *variables*.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
## Constants
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
Constants are immutable by design, which means their values cannot be
|
|
|
|
reassigned. Put in another way, they can be assigned once, at their
|
|
|
|
declaration. When defining a constant you need to provide a `name`,
|
|
|
|
`type` and a `value`:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```pascaligo group=a
|
|
|
|
const age : int = 25
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can evaluate the constant definition above using the following CLI
|
|
|
|
command:
|
2019-11-08 03:19:27 +04:00
|
|
|
```shell
|
2020-02-05 19:28:40 +04:00
|
|
|
ligo evaluate-value gitlab-pages/docs/language-basics/src/variables-and-constants/const.ligo age
|
2019-11-08 03:19:27 +04:00
|
|
|
# Outputs: 25
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```cameligo group=a
|
|
|
|
let age : int = 25
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can evaluate the constant definition above using the following CLI
|
|
|
|
command:
|
2019-11-08 03:19:27 +04:00
|
|
|
```shell
|
2020-02-05 19:28:40 +04:00
|
|
|
ligo evaluate-value gitlab-pages/docs/language-basics/src/variables-and-constants/const.mligo age
|
2019-11-08 03:19:27 +04:00
|
|
|
# Outputs: 25
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```reasonligo group=a
|
|
|
|
let age : int = 25;
|
2019-12-10 17:47:31 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can evaluate the constant definition above using the following CLI
|
|
|
|
command:
|
2019-12-10 17:47:31 +04:00
|
|
|
```shell
|
2020-02-05 19:28:40 +04:00
|
|
|
ligo evaluate-value gitlab-pages/docs/language-basics/src/variables-and-constants/const.religo age
|
2019-12-10 17:47:31 +04:00
|
|
|
# Outputs: 25
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
## Variables
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
<Syntax syntax="pascaligo">
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-10 22:07:20 +04:00
|
|
|
Variables, unlike constants, are *mutable*. They cannot be declared in
|
|
|
|
a *global scope*, but they can be declared and used within functions,
|
|
|
|
or as function parameters.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
> ⚠️ Please be wary that mutation only works within the function scope
|
2020-02-25 21:07:53 +04:00
|
|
|
> itself, values outside of the function scope will not be
|
|
|
|
> affected. In other words, when a function is called, its arguments
|
|
|
|
> are copied, *as well as the environment*. Any side-effect to that
|
|
|
|
> environment is therefore lost when the function returns.
|
2019-11-19 04:44:36 +04:00
|
|
|
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```pascaligo group=b
|
|
|
|
// The following is invalid: use `const` for global values instead.
|
2020-02-10 22:07:20 +04:00
|
|
|
// var four : int := 4
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
function add (const a : int; const b : int) : int is
|
|
|
|
block {
|
2020-02-12 01:29:12 +04:00
|
|
|
var c : int := a + 2*b;
|
|
|
|
c := c - b
|
2020-02-05 19:28:40 +04:00
|
|
|
} with c
|
2019-11-08 03:19:27 +04:00
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
> ⚠️ Notice the assignment operator `:=` for `var`, instead of `=` for
|
|
|
|
> constants.
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `add` function defined above using the LIGO compiler
|
|
|
|
like this:
|
2019-11-08 03:19:27 +04:00
|
|
|
|
|
|
|
```shell
|
2020-02-05 19:28:40 +04:00
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/variables-and-constants/add.ligo add '(1,1)'
|
2019-11-08 03:19:27 +04:00
|
|
|
# Outputs: 2
|
|
|
|
```
|
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="cameligo">
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
As expected in the pure subset of a functional language, CameLIGO only
|
2020-02-10 22:07:20 +04:00
|
|
|
features *constant values*: once they are declared, the value cannot
|
|
|
|
be changed (or "mutated").
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```cameligo group=c
|
|
|
|
let add (a : int) (b : int) : int =
|
2019-11-08 03:19:27 +04:00
|
|
|
let c : int = a + b in c
|
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `add` function defined above using the LIGO compiler
|
|
|
|
like this:
|
2019-11-08 03:19:27 +04:00
|
|
|
```shell
|
2020-02-05 19:28:40 +04:00
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/variables-and-constants/add.mligo add '(1,1)'
|
2019-11-08 03:19:27 +04:00
|
|
|
# Outputs: 2
|
|
|
|
```
|
2020-03-04 17:19:00 +04:00
|
|
|
|
|
|
|
</Syntax>
|
|
|
|
<Syntax syntax="reasonligo">
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
As expected in the pure subset of a functional language, ReasonLIGO
|
2020-02-10 22:07:20 +04:00
|
|
|
only features *constant values*: once they are declared, the value
|
2020-02-05 19:28:40 +04:00
|
|
|
cannot be changed (or "mutated").
|
2019-12-10 17:47:31 +04:00
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
```reasonligo group=c
|
|
|
|
let add = ((a, b): (int, int)): int => {
|
|
|
|
let c : int = a + b;
|
2019-12-10 17:47:31 +04:00
|
|
|
c;
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2020-02-05 19:28:40 +04:00
|
|
|
You can run the `add` function defined above using the LIGO compiler
|
|
|
|
like this:
|
2019-12-10 17:47:31 +04:00
|
|
|
```shell
|
2020-02-05 19:28:40 +04:00
|
|
|
ligo run-function gitlab-pages/docs/language-basics/src/variables-and-constants/add.religo add '(1,1)'
|
2019-12-10 17:47:31 +04:00
|
|
|
# Outputs: 2
|
|
|
|
```
|
2019-11-08 03:19:27 +04:00
|
|
|
|
2020-03-04 17:19:00 +04:00
|
|
|
</Syntax>
|
|
|
|
|