---
title: "Functions in TypeShade and what they lower to"
description: "Helpers, arrow functions, overloads, recursion, entry points and the calls the compiler refuses, each with the shader text written for it."
url: https://typeshade.dev/guide/language/from-typescript/functions/
language: en
---

# Functions

Every function is a function of the module. There are no function values, no environment to close over and no call stack.

## Parameters and names

- ### Shadowing a module value
  A parameter or body local can use the name of a module value. Closures resolve the nearest declaration; duplicates in the same scope remain errors.
  TypeScript
  ```ts
  const gain: f32 = 0.25;
  function scale(gain: f32): f32 { return gain * 2.; }
  export function answer(): f32 {
    const gain: f32 = 1.;
    return scale(gain);
  }
  ```
  Emitted WGSL
  ```wgsl
  fn scale(gain: f32) -> f32 {
    return (gain * 2.0);
  }

  fn answer() -> f32 {
    return scale(1.0);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float scale(float gain) {
    return (gain * 2.0);
  }

  float answer() {
    return scale(1.0);
  }
  ```
- ### Parameter reassignment
  A number, a `bool`, a vector and a matrix are values, as in JavaScript. The function writes a local copy that starts as the input, so the caller’s variable keeps its value. To give a changed value back, return it.
  TypeScript
  ```ts
  export function brighter(value: f32): f32 {
    value += 0.25;
    return value;
  }
  ```
  Emitted WGSL
  ```wgsl
  fn brighter(value: f32) -> f32 {
    var value_1: f32 = value;
    value_1 += 0.25;
    return value_1;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float brighter(float value) {
    float value_1 = value;
    value_1 += 0.25;
    return value_1;
  }
  ```
- ### Two results
  A function that computes two values returns a list, and the caller takes the list apart with `[lo, hi] = swap(lo, hi)`. WGSL writes the list into a local `array<f32, 2>` through a pointer, and GLSL ES 3.00 returns a `float[2]`. [reference-parameters](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/reference-parameters/reference-parameters.shade.ts)
  TypeScript
  ```ts
  function swap(a: f32, b: f32): [f32, f32] {
    return [b, a]
  }
  export function order(x: f32, y: f32): f32 {
    let lo = x
    let hi = y
    if (lo > hi) {
      [lo, hi] = swap(lo, hi)
    }
    return hi - lo
  }
  ```
  Emitted WGSL
  ```wgsl
  fn order(x: f32, y: f32) -> f32 {
    var lo: f32 = x;
    var hi: f32 = y;
    if ((lo > hi)) {
      var _list: array<f32, 2>;
      swap_into(&_list, lo, hi);
      lo = _list[0u];
      hi = _list[1u];
    }
    return (hi - lo);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float order(float x, float y) {
    float lo = x;
    float hi = y;
    if ((lo > hi)) {
      float[2] _list = swap(lo, hi);
      lo = _list[0u];
      hi = _list[1u];
    }
    return (hi - lo);
  }
  ```
- ### Object parameter
  A struct, a class instance and an array are objects, as in JavaScript. A function that writes a field or an element of its parameter changes the caller's object, and the call passes the variable with no mark. The compiler reads which parameters a function writes: WGSL takes a pointer and GLSL ES 3.00 an `inout` parameter, the way a method that writes `this` does. [reference-parameters](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/reference-parameters/reference-parameters.shade.ts)
  TypeScript
  ```ts
  class Ray {
    origin: vec2
    dir: vec2
  }
  function advance(r: Ray, t: f32): void {
    r.origin = r.origin + r.dir * t
  }
  export function moved(o: vec2, d: vec2, t: f32): vec2 {
    let r = new Ray()
    r.origin = o
    r.dir = d
    advance(r, t)
    return r.origin
  }
  ```
  Emitted WGSL
  ```wgsl
  fn advance(r: ptr<function, Ray>, t: f32) {
    (*r).origin = ((*r).origin + ((*r).dir * t));
  }

  fn moved(o: vec2<f32>, d: vec2<f32>, t: f32) -> vec2<f32> {
    var r: Ray;
    Ray_new_into(&r);
    r.origin = o;
    r.dir = d;
    advance(&r, t);
    return r.origin;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  void advance(inout Ray r, float t) {
    r.origin = (r.origin + (r.dir * t));
  }

  vec2 moved(vec2 o, vec2 d, float t) {
    Ray r = Ray_new();
    r.origin = o;
    r.dir = d;
    advance(r, t);
    return r.origin;
  }
  ```
- ### One object under two names
  Refused
  One call may not hand one variable to two parameters when its function writes either. Inside the function the two names would be one object, which the compiler does not support yet ([`TS8074`](https://typeshade.dev/reference/errors/ts8074/)). Pass two variables, or give the function one parameter for it.
  TypeScript
  ```ts
  export function twice(o: vec2): vec2 {
    let r = new Ray()
    r.origin = o
    pull(r, r)
    return r.origin
  }
  ```
  Compiler diagnostic
  **[TS8074](https://typeshade.dev/reference/errors/ts8074/)** This call hands "r" to "pull" twice, and "pull" writes one of the two: one object under two names inside "pull", which is not supported yet (change 0142, stage 2). Pass two variables, or give "pull" one parameter for it.

## Kinds of function

- ### Top-level function
  A module function of the same name, parameters and return type.
  TypeScript
  ```ts
  export function lum(c: vec3): f32 {
    return dot(c, vec3(0.2126, 0.7152, 0.0722))
  }
  ```
  Emitted WGSL
  ```wgsl
  fn lum(c: vec3<f32>) -> f32 {
    return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float lum(vec3 c) {
    return dot(c, vec3(0.2126, 0.7152, 0.0722));
  }
  ```
- ### Local function
  A function of the module, named after the function that declares it.
  TypeScript
  ```ts
  export function ramp(x: f32): f32 {
    const twice = (v: f32): f32 => v * 2.
    return twice(x)
  }
  ```
  Emitted WGSL
  ```wgsl
  fn ramp_twice(v: f32) -> f32 {
    return (v * 2.0);
  }

  fn ramp(x: f32) -> f32 {
    return ramp_twice(x);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float ramp_twice(float v) {
    return (v * 2.0);
  }

  float ramp(float x) {
    return ramp_twice(x);
  }
  ```
- ### Closure over a name
  The local function takes each name it reads from the function around it as a parameter, and a name it writes by reference.
  TypeScript
  ```ts
  export function ramp(x: f32): f32 {
    const twice = (v: f32): f32 => v * x
    return twice(x)
  }
  ```
  Emitted WGSL
  ```wgsl
  fn ramp_twice(x: f32, v: f32) -> f32 {
    return (v * x);
  }

  fn ramp(x: f32) -> f32 {
    return ramp_twice(x, x);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float ramp_twice(float x, float v) {
    return (v * x);
  }

  float ramp(float x) {
    return ramp_twice(x, x);
  }
  ```
- ### Default argument
  Every parameter stays, and the argument left out is written at the call. [default-args](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/default-args/default-args.shade.ts)
  TypeScript
  ```ts
  function vignette(
    uv: vec2, strength: f32 = 0.8
  ): f32 {
    return 1. - length(uv) * strength
  }
  ```
  Emitted WGSL
  ```wgsl
  fn vignette(uv: vec2<f32>, strength: f32) -> f32 {
    return (1.0 - (length(uv) * strength));
  }

  fn shade(uv: vec2<f32>) -> f32 {
    return vignette(uv, 0.8);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float vignette(vec2 uv, float strength) {
    return (1.0 - (length(uv) * strength));
  }

  float shade(vec2 uv) {
    return vignette(uv, 0.8);
  }
  ```
- ### Optional value
  Function results may be `T | undefined`, `T | null` or `T | null | undefined` ([Rule 7.10](https://typeshade.dev/reference/rules/7-10/)). Inference preserves the absent states of returned values, including callbacks and local functions. A bare return or fall-through supplies null for a null-only result and undefined when admitted. Strict tests distinguish states; `??` evaluates its fallback only for an absent value. An un-narrowed read is [`TS8003`](https://typeshade.dev/reference/errors/ts8003/). [Nullable values](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/null-values/null-values.shade.ts)
  TypeScript
  ```ts
  function root(x: f32): f32 | undefined {
    if (x < 0.) {
      return undefined
    }
    return sqrt(x)
  }
  ```
  Emitted WGSL
  ```wgsl
  fn root_into(_dst: ptr<function, _opt_f32_>, x: f32) {
    if ((x < 0.0)) {
      (*_dst) = _opt_f32_(0.0, false);
      return;
    }
    (*_dst) = _opt_f32_(sqrt(x), true);
  }

  fn rootOr(x: f32) -> f32 {
    var _opt: _opt_f32_;
    root_into(&_opt, x);
    return select(1.0, _opt.value, _opt.present);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  _opt_f32_ root(float x) {
    if ((x < 0.0)) {
      return _opt_f32_(0.0, false);
    }
    return _opt_f32_(sqrt(x), true);
  }

  float rootOr(float x) {
    _opt_f32_ _opt = root(x);
    return mix(1.0, _opt.value, _opt.present);
  }
  ```
- ### Overload signature
  The signatures are skipped and the implementation is lowered once.
  TypeScript
  ```ts
  export function lum(c: vec3): f32
  export function lum(c: vec3): f32 {
    return dot(c, vec3(0.2126, 0.7152, 0.0722))
  }
  ```
  Emitted WGSL
  ```wgsl
  fn lum(c: vec3<f32>) -> f32 {
    return dot(c, vec3<f32>(0.2126, 0.7152, 0.0722));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float lum(vec3 c) {
    return dot(c, vec3(0.2126, 0.7152, 0.0722));
  }
  ```
- ### Recursive call
  Refused
  WGSL has no call stack, and the check reads the call graph.
  TypeScript
  ```ts
  export function fall(x: f32): f32 {
    return fall(x * 0.5)
  }
  ```
  Compiler diagnostic
  **[TS8031](https://typeshade.dev/reference/errors/ts8031/)** Recursive call: "fall" -> "fall". WGSL has no call stack, so a function must not take part in a call cycle.

## Calls

- ### Call as a statement
  A statement of its own. A value-returning builtin standing alone computes nothing, so the optimizer drops it.
  TypeScript
  ```ts
  @compute([64, 1, 1])
  export function main_k(
    gid: builtin<"global_invocation_id">
  ): void {
    store(gid.x, 1.)
  }
  ```
  Emitted WGSL
  ```wgsl
  @compute @workgroup_size(64)
  fn main_k(@builtin(global_invocation_id) gid: vec3<u32>) {
    store(gid.x, 1.0);
  }
  ```
  The compiler emits no GLSL for this construct. The backend says:
  backend 'glsl-es300' cannot emit this module — missing capabilities: storageBuffer, compute
- ### Call with an effect
  A builtin that returns a value and has an effect takes WGSL's phony assignment, `_ =`, since Tint treats it as must-use. [atomic-histogram](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/atomic-histogram/atomic-histogram.shade.ts)
  TypeScript
  ```ts
  @compute([64, 1, 1])
  export function main_k(
    gid: builtin<"global_invocation_id">
  ): void {
    atomicAdd(hits[gid.x], 1)
  }
  ```
  Emitted WGSL
  ```wgsl
  @compute @workgroup_size(64)
  fn main_k(@builtin(global_invocation_id) gid: vec3<u32>) {
    _ = atomicAdd(&hits[gid.x], 1u);
  }
  ```
  The compiler emits no GLSL for this construct. The backend says:
  backend 'glsl-es300' cannot emit this module — missing capabilities: storageBuffer, compute
- ### `Math` names
  `Math.sin` is the same builtin as `sin`, and `Math.PI` folds to its value.
  TypeScript
  ```ts
  export function wave(x: f32): f32 {
    return Math.sin(x * Math.PI)
  }
  ```
  Emitted WGSL
  ```wgsl
  fn wave(x: f32) -> f32 {
    return sin((x * 3.141592653589793));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float wave(float x) {
    return sin((x * 3.141592653589793));
  }
  ```
