From 626054ed668c64ec5200118dd8e12ee032c081b3 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Wed, 18 Mar 2020 09:49:35 +0100 Subject: [PATCH] Describe nested functions --- .../docs/language-basics/functions.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/gitlab-pages/docs/language-basics/functions.md b/gitlab-pages/docs/language-basics/functions.md index 5d34a5e74..f8fc729a6 100644 --- a/gitlab-pages/docs/language-basics/functions.md +++ b/gitlab-pages/docs/language-basics/functions.md @@ -299,6 +299,41 @@ gitlab-pages/docs/language-basics/src/functions/incr_map.religo incr_map + +## Nested functions (also known as closures) +It's possible to place functions inside other functions. These functions +have access to variables in the same scope. + + + +```pascaligo +function closure_example (const i : int) : int is + block { + function closure (const j : int) : int is i + j + } with closure (i) +``` + + + + +```cameligo +let closure_example (i : int) : int = + let closure : int -> int = fun (j : int) -> i + j in + closure i +``` + + + + +```reasonligo +let closure_example = (i : int) : int => { + let closure = (j: int): int => i + j; + closure(i); +}; +``` + + + ## Recursive function LIGO functions are not recursive by default, the user need to indicate that the function is recursive.