typestar

Procedures in Odin

A tip calculator with a default parameter and named call-site arguments.

package main

import "core:fmt"

// Parameters can carry default values; :: binds the proc to a name.
tip :: proc(total: f64, pct: f64 = 20.0) -> f64 {
    return total * pct / 100.0
}

main :: proc() {
    fmt.printfln("default tip on 48.60: %.2f", tip(48.60))
    fmt.printfln("generous tip: %.2f", tip(48.60, 25.0))
    // Arguments can be named at the call site for clarity.
    fmt.printfln("named: %.2f", tip(total = 12.75, pct = 15.0))
}

How it works

  1. tip :: proc(total: f64, pct: f64 = 20.0) gives the rate a default.
  2. Calling tip(48.60) uses it; tip(48.60, 25.0) overrides positionally.
  3. tip(total = 12.75, pct = 15.0) names its arguments for the reader.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
424
Tokens
89
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 76 seconds.

Type this snippet

Step 1 of 3 in Procedures, step 7 of 27 in Language basics.

← Previous Next →

Procedures in other languages