---
title: "TypeShade functions"
description: "A detailed guide to TypeShade functions: TypeScript-style parameters and returns, helpers, shader entries, stages and explicit builtins."
url: https://typeshade.dev/guide/language/functions/
language: en
---

# Functions: from TypeScript functions to GPU functions

Functions are the basic unit for naming computation and making inputs and outputs explicit. TypeShade keeps the TypeScript function shape, but every function in a `"use typeshade"` file must describe computation that can be lowered to GPU IR.

## 1. Anatomy of a function

A function has a name, parameter list, return type and body. TypeShade keeps that familiar structure while giving parameters and return values GPU semantics.

```ts
function addBias(value: f32): f32 {
  return value + 0.5
}
```

Here `value` is the input parameter, `f32` is the GPU type of both the input and result, and `return` produces the value for the caller.

## 2. Parameters and return types

A parameter is an input to the function and the return type describes the shape of its result. A number, a `bool`, a vector or a matrix is a value, as in JavaScript: assigning to such a parameter changes a local copy for that invocation and leaves the caller’s variable unchanged. A struct, a class instance or an array is an object, and the next paragraph says what a function may do with one. Types tell the compiler which values and operations are valid. Helpers accept nullable parameters and results, including generic functions and function-typed callbacks. `value?: T | null` admits both absent states. Nullable values remain outside entry IO and kernel interfaces.

```ts
function mix(a: vec4, b: vec4, amount: f32): vec4 {
  return a + (b - a) * amount
}
```

| Part | Role |
| --- | --- |
| `a`, `b` | GPU input values. |
| `amount: f32` | A scalar input whose type participates in expression checking. |
| `: vec4` | The GPU value shape returned to the caller. |

A function that computes one number, vector or matrix returns it. A function that computes two returns a list, and the caller takes the list apart with a list assignment, `[lo, hi] = swap(lo, hi)`. An object is different. A struct, a class instance or an array has the identity a JavaScript object has, so a function that writes a field or an element of its parameter changes the caller's object, and neither the parameter nor the call carries a mark. The compiler reads which parameters a function writes and passes each one as the caller's object, as a pointer in WGSL and as an `inout` parameter in GLSL ES 3.00; you write neither. `@inout` and `@out` are removed and refused ([`TS8073`](https://typeshade.dev/reference/errors/ts8073/)). One call may not hand one variable to two parameters when the function writes either ([`TS8074`](https://typeshade.dev/reference/errors/ts8074/), not supported yet). In this version only a function declared at the top of the file or in a namespace writes the object it is given: a method, a constructor, a setter, a local function, a function handed as an argument and a generic function take their parameters as values, and writing an object parameter there is [`TS8018`](https://typeshade.dev/reference/errors/ts8018/). A local function inside such a function still reads and writes the parameter, as it does any variable around it. [Objects are passed as JavaScript passes them](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/reference-parameters/reference-parameters.shade.ts) shows a struct moved in place, an array written through a local function and two numbers put in order.

```ts
function swap(a: f32, b: f32): [f32, f32] {
  return [b, a]
}

class Ray {
  origin: vec2
  dir: vec2
}

function advance(r: Ray, t: f32): void {
  r.origin = r.origin + r.dir * t
}

function spread(x: f32, y: f32, o: vec2, d: vec2): f32 {
  let lo = x
  let hi = y
  if (lo > hi) {
    [lo, hi] = swap(lo, hi)
  }
  const ray = new Ray()
  ray.origin = o
  ray.dir = d
  advance(ray, hi - lo)
  return ray.origin.x
}
```

## 3. Helper functions

A top-level function with no stage attribute is a helper. Helpers let you name repeated calculations and keep shader entries focused on pipeline inputs, resources and outputs.

```ts
function saturate(x: f32): f32 {
  return clamp(x, 0.0, 1.0)
}

function remap(x: f32): f32 {
  return saturate(x * 2.0 - 1.0)
}
```

A helper has no stage attribute because it is not a pipeline entry. This keeps reusable math separate from the pipeline interface.

## 4. Calling a function

A call looks like an ordinary TypeScript call, but its callee and arguments must stay inside the TypeShade GPU type model. Do not treat the shader as a place to call arbitrary JavaScript APIs.

```ts
function addBias(value: f32): f32 {
  return value + 0.5
}

function shade(value: f32): f32 {
  const biased = addBias(value)
  return clamp(biased, 0.0, 1.0)
}
```

The call looks like a normal function call, but both the argument and the result of `addBias` must be values supported by the TypeShade GPU model.

## 5. Shader entry functions

A pipeline entry is a top-level function declaration with one stage attribute before it. `@compute([64])`, `@vertex` and `@fragment` attach the function to a GPU execution stage.

```ts
@compute([64, 1, 1])
export function paint(gid: builtin<"global_invocation_id">) {
  pixels[gid.x] = addBias(camera.pos.x)
}
```

`export` keeps the familiar module surface and exposes the function to the compiler as an entry candidate. The stage attribute adds the GPU-specific stage information.

## 6. Builtins are parameters

GPU-provided stage inputs are explicit function parameters, not hidden global variables. Reading the signature tells you exactly which external inputs the entry expects.

```ts
@compute([64, 1, 1])
export function paint(
  gid: builtin<"global_invocation_id">
) {
  const i = gid.x
  pixels[i] = pixels[i] + camera.pos.x
}
```

| Form | Meaning |
| --- | --- |
| `builtin<"global_invocation_id">` | Selects the GPU-provided compute input. |
| `gid` | The local name for that input, a `vec3u`. |
| `gid.x` | Reads the x component for the current invocation. |

## 7. Reading a compute entry

`@compute([64, 1, 1])` declares the workgroup size. `gid` is a parameter receiving the `global_invocation_id` builtin, and `gid.x` reads the current invocation’s x coordinate.

```ts
@compute([64, 1, 1])
export function paint(gid: builtin<"global_invocation_id">) {
  const i = gid.x
  pixels[i] = pixels[i] + camera.pos.x
}
```

When reading this function, first identify the stage and workgroup size, then inspect the parameters for external GPU inputs, and finally follow the body’s calculation.

## 8. Vertex and fragment entries

Graphics stages use the same function model. The stage attribute selects the stage, while parameters and the return type describe the pipeline interface.

```ts
@vertex
export function vs(
  vid: builtin<"vertex_index">,
  vin: VsIn
): vec4 {
  return camera.view * vec4(vin.position, 1)
}

@fragment
export function fs(pid: builtin<"position">): vec4 {
  return vec4(pid.x, 0, 0, 1)
}
```

The vertex function has two different inputs: `vid` is a GPU builtin and `vin` is a user-defined struct. The fragment function makes its builtin input, `pid`, explicit as well, so the signature documents the stage interface.

## 9. Functions and scope

Local variables belong to the current function invocation. Parameters and body locals may shadow module values; a closure resolves the nearest declaration. Duplicate declarations in the same scope remain errors. Every declaration may use a legal TypeScript name such as \`target\` or \`half\`: a variable, a parameter, a resource, an entry, a struct and a field alike. The shader writer gives a name its target reserves a fresh spelling on that target, and the host still binds and calls each one by the name written.

```ts
function exposure(color: vec3, amount: f32): vec3 {
  const factor = amount + 1.0
  return color * factor
}
```

`factor` is a local value scoped to the function. Resources such as `camera` and `pixels` belong to the host-facing shader interface, while builtin parameters are inputs supplied by the GPU stage.

## 10. Where TypeScript functions stop

- Do not call arbitrary JavaScript runtime APIs.
- Do not assume dynamic object creation or general runtime side effects can become shader computation.
- Entry points are top-level exported functions, not class methods.
- Builtins are explicit parameters, not implicit globals.
- Parameters and return types must have valid GPU value semantics.

## 11. From a small helper to a real entry

The example below keeps reusable math in a helper and lets the entry connect the builtin input and resources.

```ts
"use typeshade";

function addBias(value: f32): f32 {
  return value + 0.5;
}

@compute([64, 1, 1])
export function paint(gid: builtin<"global_invocation_id">) {
  const value = camera.pos.x;
  pixels[gid.x] = addBias(value);
}
```

The important distinction is the boundary: `addBias` is reusable computation, while `paint` connects the stage, builtin input and resources to an actual GPU invocation.

[Next: Control flow](https://typeshade.dev/guide/language/control-flow/)
