# call() function

> The call() function declares one call and the fields of its args, for defineCalls().

The **`call()`** function declares one call and the fields of its args, for [`defineCalls()`](/docs/room/definecalls).

## Syntax

```ts
call()
call(fields)
```

### Parameters

`fields` Optional
: An object of the args' fields, each a field type as [`entity()`](/docs/room/entity) takes, but `"bytes"`, or a declaration from [`json<T>()`](/docs/room/json) without an initial value or [`string(max)`](/docs/room/string). Without it, the call takes no args.

### Return value

A [`CallDeclaration`](/docs/room/call-declaration). Its `returns<T>()` declares the reply's type.

### Exceptions

`TypeError`
: Thrown for a `bytes` field, a `json` field with an initial value, or an unknown field type.

`RangeError`
: Thrown for a `string(max)` with a `max` out of range.

## Description

The room checks a call declared with fields before its handler runs: each field must be there and pass the check a write of its field type gets, and an `f32`, `f64` or `angle` must also be finite. Keys that aren't declared are dropped. A call declared with `call()` gets `undefined` whatever the client sends.

## Examples

### Calls with and without args

```ts
// inventory.ts
import { call, defineCalls, defineRoom, defineSchema, entity, json } from "@irtio/room";

const Bag = entity("bag", { items: json<string[]>([]) });
const schema = defineSchema({
  private: Bag,
  calls: defineCalls({
    drop: call({ slot: "u8" }).returns<string[]>(),
    sort: call({ order: json<"name" | "rarity">() }),
    contents: call().returns<string[]>(),
  }),
});

export default defineRoom({
  schema,
  calls: {
    drop({ slot }, { room, connection }) {
      const bag = room.private(connection);
      bag.items = bag.items.filter((_item, i) => i !== slot);
      return bag.items;
    },
    sort({ order }, { room, connection }) {
      const bag = room.private(connection);
      if (order === "name") bag.items = [...bag.items].sort();
    },
    contents: (_args, { room, connection }) => room.private(connection).items,
  },
});
```

## See also

- [`CallDeclaration`](/docs/room/call-declaration)
- [Declaring calls](/docs/room/guides/calls#declaring-calls)
