API Reference › Program structure › fn
keyword

fn

fn name

fn name names a top-level function as a value, so it can be handed to something that calls it later - today, Job.parallel_for, which runs it on worker threads. It is a plain function reference: no closure and nothing captured, so everything the function needs is passed to it (for Job.parallel_for, through its ctx argument). A worker function takes (i: int, ctx) and returns void; the compiler rejects any other shape.

Example

program Squares {
  function square(i: int, out: words) -> void { out[i] = i * i }

  entry {
    let sq = words(1000)
    Job.parallel_for(1000, fn square, sq)
    print(sq[12])
  }
}
← All symbols