Browse Source

Document inline functions

pull/95/head
Garrit Franke 1 month ago
parent
commit
39d4688e61
No known key found for this signature in database
GPG Key ID: A28BC0D6B31A2673
  1. 30
      docs/concepts/functions.md

30
docs/concepts/functions.md

@ -66,3 +66,33 @@ fn add_one(x: int): int {
$ sb run main.sb
2
```
# Simplified Function Syntax for Single Statements
Antimony supports a more concise syntax for functions that perform a single operation. This syntax is particularly useful for simple tasks, such as arithmetic operations, printing to the console, or returning a single expression. Instead of wrapping the function's body in curly braces, you can define the function using an equals sign (`=`) followed by the expression that constitutes the function's body.
## Syntax
The syntax for this simplified function declaration is as follows:
```
fn function_name(parameters): return_type = expression
```
This syntax removes the need for curly braces and the `return` keyword for single-statement functions, making the code cleaner and more readable.
## Examples
Below are examples demonstrating how to use this syntax:
**Defining a function that adds two numbers**:
```
fn add(x: int, y: int): int = x + y
```
**Defining a function that concatenates two strings**:
```
fn concat(a: string, b: string): string = a + b
```

Loading…
Cancel
Save