| 1 |
# Function declaration |
| 2 |
|
| 3 |
Function definition is started with the `func` keyword optionally followed by an |
| 4 |
identifier, and a list of arguments enclosed in parenthesis. If the function |
| 5 |
returns a value it must specify the type of that value by adding `-> <type>` |
| 6 |
after the argument list. |
| 7 |
|
| 8 |
- The identifier is _required_ if the function definition is a statement. |
| 9 |
- The identifier is _not allowed_ if the function definition is in an expression. |
| 10 |
|
| 11 |
Positional and keyword arguments are supported in the argument list. Positional |
| 12 |
arguments must come first. Keyword arguments are specified by adding a colon |
| 13 |
(:) to the end of the argument followed by an optional default value. All |
| 14 |
arguments must specify their type by following the argument name with a type |
| 15 |
specifier. |
| 16 |
|
| 17 |
Function definition is ended with the `endfunc` keyword. |
| 18 |
|
| 19 |
Examples: |
| 20 |
|
| 21 |
```meson |
| 22 |
func a() -> int |
| 23 |
return 1 |
| 24 |
endfunc |
| 25 |
|
| 26 |
func b(arg1 int) -> int |
| 27 |
return arg1 |
| 28 |
endfunc |
| 29 |
|
| 30 |
func c(arg1 int, kw str:, kw_with_default str: 'default') |
| 31 |
message(arg1) |
| 32 |
message(kw) |
| 33 |
message(kw_with_default) |
| 34 |
endfunc |
| 35 |
|
| 36 |
d = func() |
| 37 |
message('hello from an anonymous function!') |
| 38 |
endfunc |
| 39 |
``` |