# Stream functions

Stream functions are [function types](../index.md#fun_types) which return zero or more output rows.

## [](#_syntax)Syntax

Stream functions can be identified by their return type containing curly braces `{}`.

Scalar stream function return format

```typeql
{ <return-type> }
```

Tuple stream function return format

```typeql
{ <return-type-0>, ..., <return-type-N> }
```

## [](#_usage)Usage

Stream functions are defined and called like any other function. To retrieve the results of the called function, considering that the number of results for a single call is undefined, use the keyword `in`. .Schema for the following examples

Details

```typeql
#!test[schema, commit]
define
  entity content owns id;
  entity page sub content,
    owns page-id,
    owns name;
  entity profile sub page,
    owns username;
  entity user sub profile,
    owns phone,
    owns karma;

  attribute id value string;
  attribute page-id sub id;
  attribute username sub page-id;
  attribute name value string;
  attribute phone value string;
  attribute karma value double;
```

### [](#scalar_functions)Scalar functions

Stream scalar functions are used to return multiple results for one expression: a variable or a function call. The following examples illustrate simple usages of functions for retrieving a stream of `type` s and a stream of `integer` s.

```typeql
#!test[schema, commit]
define
  fun user_phones($user: user) -> { phone }:
    match
      $user has phone $phone;
    return { $phone };
```

```typeql
#!test[schema, commit]
define
  fun add_streamed($x: integer, $y: integer) -> { integer }:
    match
      let $z = $x + $y;
    return { $z };
```

To retrieve the results of such functions calls, the following read query can be used:

```typeql
#!test[read]
match
  $user isa user;
  let $phone in user_phones($user);
```

### [](#_tuple_functions)Tuple functions

It is possible to add another dimension to the returned type and enumerate a number of results returned from a function using commas. Tuples can contain any combinations of scalar returned values.

```typeql
#!test[schema, commit]
define
  fun all_users_and_phones() -> { user, phone, string }:
    match
      $user isa user, has phone $phone;
      let $phone-value = $phone;
    return { $user, $phone, $phone-value };
```

To retrieve the results of such functions calls, the following read query can be used:

```typeql
#!test[read]
match
  let $user, $phone, $phone-value in all_users_and_phones();
```

[Writing functions](../writing/index.md) [Scalars and tuples](../scalar/index.md)

[Edit on GitHub](https://github.com/typedb/typedb-docs/edit/3.x-development/typeql-reference/modules/ROOT/pages/functions/stream.adoc) Edit this page on GitHub.