Learn Tin · Part 1: The language · Chapter 3
Functions
Parameters, return types and functions that are one expression.
Defining a function
A function takes values in (its parameters) and gives one value back (its result). Every parameter has a type, and so does the result.
tin run define.tin// fn name(parameter: type, ...): result type { body }
fn add(a: int, b: int): int {
a + b // the last expression is the result
}
fn main() {
print(add(2, 3))
let total = add(add(1, 2), 10)
print(total)
} 5
13 There is no return at the end. In Tin, a block's last expression is its value, and a
function's body is a block. That one rule shows up everywhere, in if and match too.
One-line functions
When the body is a single expression, write it after => and skip the braces.
tin run arrow.tinfn square(x: int): int => x * x
fn greeting(name: string): string => "Hello, {name}!"
fn main() {
print(square(12))
print(greeting("Ada"))
} 144
Hello, Ada! Functions that only do something
Leave out the result type when a function is there for what it does, like printing, rather than for a value.
tin run no-result.tin// No result type: the function does something instead of computing a value.
fn banner(title: string) {
let line = "=".repeat(title.len + 4)
print(line)
print("| {title} |")
print(line)
}
fn main() {
banner("Tin")
} =======
| Tin |
======= Leaving early with return
return exits the function right away with a value. It's useful for handling the special cases
first, so the main case reads cleanly at the end.
tin run return.tinfn describe(n: int): string {
if n < 0 { return "negative" } // return leaves the function early
if n == 0 { return "zero" }
"positive"
}
fn main() {
print(describe(-4))
print(describe(0))
print(describe(9))
} negative
zero
positive A function that calls itself
tin run recursion.tin// A function can call itself.
fn factorial(n: int): int => if n <= 1 { 1 } else { n * factorial(n - 1) }
fn main() {
print(factorial(5))
print(factorial(12))
} 120
479001600 factorial(13) is bigger than an int can hold, so it panics instead of printing a wrong number.
Mistakes the compiler catches
Parameters always need types. This is what lets Tin check every call, and later it is how an agent's tools get their schemas.
tin check param-types.tinfn add(a, b) => a + b
fn main() {
print(add(1, 2))
} error: expected ':' after parameter 'a' (parameters need types), found ','
--> param-types.tin:1:9
|
1 | fn add(a, b) => a + b
| ^ A call must pass every argument. The note shows the function's signature, so you don't have to look it up:
tin check arity.tinfn add(a: int, b: int): int => a + b
fn main() {
print(add(1))
} error: add takes 2 argument(s), found 1
--> arity.tin:4:9
|
4 | print(add(1))
| ^^^^^^
= note: signature: add(a: int, b: int) A function that promises an int must produce one on every path. An if without an
else has no value when the condition is false:
tin check missing-value.tinfn bonus(score: int): int {
if score > 100 { 10 }
}
fn main() {
print(bonus(120))
} error: 'bonus' must return int, but its body ends without a value
--> missing-value.tin:1:1
|
1 | fn bonus(score: int): int {
| ^^^^^^^^
= note: the last expression of a block is its value; or use `return` Tasks
●○○ Task 1: The largest of three
Write max3(a: int, b: int, c: int): int and print it for (3, 9, 4),
(-1, -5, -2) and (7, 7, 7).
Solution
tin run task-1.solution.tinfn max3(a: int, b: int, c: int): int {
let ab = if a > b { a } else { b }
if ab > c { ab } else { c }
}
fn main() {
print(max3(3, 9, 4))
print(max3(-1, -5, -2))
print(max3(7, 7, 7))
} 9
-1
7 ●●○ Task 2: Leap years
A year is a leap year if it divides by 4, except years that divide by 100, unless they also divide by
400. Write isLeap(year: int): bool as a one-line function and check 2024, 1900, 2000 and 2026.
Solution
tin run task-2.solution.tin// Divisible by 4, except centuries, except every 400 years.
fn isLeap(year: int): bool => year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
fn main() {
print("2024: {isLeap(2024)}")
print("1900: {isLeap(1900)}")
print("2000: {isLeap(2000)}")
print("2026: {isLeap(2026)}")
} 2024: true
1900: false
2000: true
2026: false ●●○ Task 3: Every path needs a value
This doesn't compile. Read the error and fix grade so that scores below 75 get
"C".
tin check task-3.tinfn grade(score: int): string {
if score >= 90 { "A" }
else if score >= 75 { "B" }
}
fn main() {
print(grade(80))
} error: branches have different types: string and void
--> task-3.tin:2:3
|
2 | if score >= 90 { "A" }
| ^^^^^^^^^^^^^^^^^^^^^^ Solution
tin run task-3.solution.tinfn grade(score: int): string {
if score >= 90 { "A" }
else if score >= 75 { "B" }
else { "C" }
}
fn main() {
print(grade(95))
print(grade(80))
print(grade(40))
} A
B
C Without the last else, the whole if has no value when both conditions
are false. Its branches would be a string and nothing at all, which is what the error says.
What's next
Next: Control flow. if, loops and ranges. Most things are expressions.
In the reference: Functions