Functions
You will learn
The two ways t27 writes a function signature, and the body with return.
fn add(a: u8, b: u8) -> u8 declares a function with two u8 inputs and a u8 result; the body in braces ends with return. The compiler also accepts the Zig style, pub fn add(a: u8, b: u8) u8, without the arrow. There are no generics: fn id<T> is a parse error. The test runner on this site checks constants and does not call functions, so the lesson keeps the expected results as constants beside them.
Try it
Write a function sub in the player in either style and check the verdict stays clean.

The pieces of a t27 function signature in both styles the compiler accepts, and the generics it does not. Lesson 12 of the t27 basics course.
specs/basics/12_functions.t27
// SPDX-License-Identifier: Apache-2.0
; t27 basics, lesson 12: Functions.
; A function takes typed inputs and returns a typed result.
module basics_12_functions;
pub fn add(a: u8, b: u8) -> u8 {
return a + b;
}
pub fn twice(a: u8) u8 {
return a + a;
}
pub const TWO_PLUS_THREE : u8 = 5;
test results_are_kept_as_constants {
assert TWO_PLUS_THREE == 2 + 3;
}
All lessons
Module 1 · A spec file
What a .t27 file holds: a module line, prose lines and comments.
Module 2 · Values and types
Constants, the width of a whole number, and true, false and text.
Module 3 · Arrays, trits and expressions
Lists of values, the three values of a trit, and what operators compute.
Module 4 · Tests and functions
A test block, several small tests in one spec, and a function with typed inputs.
Module 5 · Inside a function
Names with let and var, choices with if and switch, and loops.
Module 6 · Visibility and your own types
What pub shares with other modules, and the structs and enums you declare.
Module 7 · Modules, rules and gen-ts
Reading another module, stating a rule that always holds, and a spec turned into TypeScript.
Module 8 · The compiler and tri
The backends t27c writes, the stages it reads a spec in, and the tri command.
Module 9 · Errors, a program, what next
Reading a compiler error, one small complete program, and the course to take next.