Browse Source

docs: document functions

github-actions
Garrit Franke 3 years ago
parent
commit
17803d8ab9
  1. 1
      TODO
  2. 34
      docs/concepts/functions.md
  3. 6
      examples/playground.sb

1
TODO

@ -6,6 +6,7 @@
- stdlib moves line of file, which makes error reporting impossible
# Features
- Expect return statement if specified in function
- Type inference
- Multi-file support
- Argument overloading

34
docs/concepts/functions.md

@ -33,10 +33,36 @@ fn another_function(x: int) {
}
```
## Functions contain statements
## Return types
TODO
Functions can optionally return a value. To specify the return type, it is added to the function signature, similar to how variables and parameters do. Here's a simple example of a function that returns an integer:
## Return types
```
fn add_one(x: int): int {}
```
Note that this function won't compile, since it doesn't actually return anything. Let's fix that by adding a `return` statement with an expression:
```
fn add_one(x: int): int {
return x + 1
}
```
Now, if you call the function with `1` as its argument and read its value, you will see the computed result:
TODO
```
fn main() {
let result = add_one(1)
println(result)
}
fn add_one(x: int): int {
return x + 1
}
```
```
$ sabre run main.sb
2
```

6
examples/playground.sb

@ -1,8 +1,8 @@
fn main() {
let x = two()
let x = add_one(2)
println(x)
}
fn two(): int {
return 2
fn add_one(x: int): int {
return x + 1
}
Loading…
Cancel
Save