diff --git a/gitlab-pages/docs/language-basics/functions.md b/gitlab-pages/docs/language-basics/functions.md index f19c96ad4..83da5a338 100644 --- a/gitlab-pages/docs/language-basics/functions.md +++ b/gitlab-pages/docs/language-basics/functions.md @@ -35,8 +35,10 @@ Here's how you define a basic function that accepts two `ints` and returns a sin ```pascaligo -function add(const a: int; const b: int): int is - block { skip } with a + b +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: @@ -44,9 +46,14 @@ The function body consists of two parts: - `block {}` - logic of the function - `with ` - 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`. +#### Blockless functions +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: + +```pascaligo +function add(const a: int; const b: int): int is a + b +``` @@ -65,3 +72,25 @@ value. + + +```pascaligo +const increment : (int -> int) = (function (const i : int) : int is i + 1); +// a = 2 +const a: int = increment(1); +``` + + +## Anonymous functions + +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/map. + +Here's how to define an anonymous function assigned to a variable `increment`, with it's appropriate function type signature. + + +```pascaligo +const increment : (int -> int) = (function (const i : int) : int is i + 1); +// a = 2 +const a: int = increment(1); +``` + \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/functions/add.ligo b/gitlab-pages/docs/language-basics/src/functions/add.ligo new file mode 100644 index 000000000..9337b1668 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/functions/add.ligo @@ -0,0 +1,4 @@ +function add(const a: int; const b: int): int is + begin + const result: int = a + b; + end with result; \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/functions/anon.ligo b/gitlab-pages/docs/language-basics/src/functions/anon.ligo new file mode 100644 index 000000000..b7f6f14f9 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/functions/anon.ligo @@ -0,0 +1,3 @@ +const increment : (int -> int) = (function (const i : int) : int is i + 1); +// a = 2 +const a: int = increment(1); \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/src/functions/blockless.ligo b/gitlab-pages/docs/language-basics/src/functions/blockless.ligo new file mode 100644 index 000000000..6745c09e8 --- /dev/null +++ b/gitlab-pages/docs/language-basics/src/functions/blockless.ligo @@ -0,0 +1 @@ +function add(const a: int; const b: int): int is a + b \ No newline at end of file diff --git a/gitlab-pages/docs/language-basics/variables-and-constants.md b/gitlab-pages/docs/language-basics/variables-and-constants.md index 55bee7a64..3823c0635 100644 --- a/gitlab-pages/docs/language-basics/variables-and-constants.md +++ b/gitlab-pages/docs/language-basics/variables-and-constants.md @@ -43,6 +43,9 @@ Variables, unlike constants, are mutable. They can't be used in a *global scope* > 💡 Don't worry if you don't understand the function syntax yet. We'll get to it in upcoming sections of the docs. +> ⚠️ Please be wary that mutation only works within the function scope itself, values outside of the function scope will not be affected. + + ```pascaligo // won't work, use const for global values instead diff --git a/gitlab-pages/website/pages/en/index.js b/gitlab-pages/website/pages/en/index.js index 3cbc9da91..a82034678 100644 --- a/gitlab-pages/website/pages/en/index.js +++ b/gitlab-pages/website/pages/en/index.js @@ -18,55 +18,25 @@ const GridBlock = CompLibrary.GridBlock; const pre = "```"; const pascaligoExampleSmall = `${pre}pascaligo -// variant defining pseudo multi-entrypoint -// actions -type action is -| Increment of int -| Decrement of int - -function add - (const a: int; const b: int): int is - block { skip } with a + b - -function subtract - (const a: int; const b: int): int - is block { skip } with a - b - -// real entrypoint that re-routes the flow -// based on the action provided -function main - (const p: action; const s: int): - (list(operation) * int) is - block { skip } - with ((nil : list(operation)), - case p of - | Increment(n) -> add(s, n) - | Decrement(n) -> subtract(s, n) - end) -${pre}`; - -const pascaligoExample = `${pre}pascaligo // variant defining pseudo multi-entrypoint actions type action is | Increment of int | Decrement of int -function add (const a : int ; const b : int) : int is - block { skip } with a + b +function add (const a : int ; const b : int) : int is a + b -function subtract (const a : int ; const b : int) : int is - block { skip } with a - b +function subtract (const a : int ; const b : int) : int is a - b -// real entrypoint that re-routes the flow based -// on the action provided -function main (const p : action ; const s : int) : - (list(operation) * int) is - block { skip } with ((nil : list(operation)), - case p of - | Increment(n) -> add(s, n) - | Decrement(n) -> subtract(s, n) - end) +// real entrypoint that re-routes the flow based on the action provided +function main (const p : action ; const s : int) : (list(operation) * int) is + ((nil : list(operation)), + case p of + | Increment (n) -> add (s, n) + | Decrement (n) -> subtract (s, n) + end) ${pre}`; + +const pascaligoExample = pascaligoExampleSmall; const cameligoExampleSmall = `${pre}ocaml type storage = int diff --git a/src/test/contracts/website2.ligo b/src/test/contracts/website2.ligo index c58561aa9..ac13b7c6f 100644 --- a/src/test/contracts/website2.ligo +++ b/src/test/contracts/website2.ligo @@ -3,15 +3,13 @@ type action is | Increment of int | Decrement of int -function add (const a : int ; const b : int) : int is - block { skip } with a + b +function add (const a : int ; const b : int) : int is a + b -function subtract (const a : int ; const b : int) : int is - block { skip } with a - b +function subtract (const a : int ; const b : int) : int is a - b // real entrypoint that re-routes the flow based on the action provided -function main (const p : action ; const s : int) : (list(operation) * int) is - block {skip} with ((nil : list(operation)), +function main (const p : action ; const s : int) : (list(operation) * int) is + ((nil : list(operation)), case p of | Increment (n) -> add (s, n) | Decrement (n) -> subtract (s, n)