---
slug: 12-tasks-lambdas
title: Tasks & lambdas
description: Define tasks with give, default and named arguments, recursion, and first-class lambdas passed to higher-order operations.
example_ids: [tasks]
---

# Tasks & lambdas

A `task` is a function: parameters, a body, and `give` to return a value.

```synsema
-- Doc example: tasks, default args, named args, recursion, lambdas.
intent: "doc example: tasks and lambdas"

task add(a, b)
    give a + b

task greet(name, greeting = "Hello")
    give greeting + ", " + name + "!"

task factorial(n)
    when n <= 1
        give 1
    otherwise
        give n * factorial(n - 1)

print(greet("Ada") + "  factorial(5) = " + text(factorial(5)))    -- run shows the result

test "call, default arg, named arg"
    assert_eq(add(2, 3), 5)
    assert_eq(greet("Ada"), "Hello, Ada!")
    assert_eq(greet("Ada", "Hi"), "Hi, Ada!")
    assert_eq(greet("Ada", greeting = "Hey"), "Hey, Ada!")

test "recursion"
    assert_eq(factorial(5), 120)

test "lambdas are values, passed to higher-order tasks"
    let triple be (n) => n * 3
    assert_eq(triple(4), 12)
    assert_eq(apply(triple, [1, 2, 3]), [3, 6, 9])
```

## Default & named arguments

```synsema
task greet(name, greeting = "Hello")
    give greeting + ", " + name + "!"

greet("Ada")                    -- uses the default
greet("Ada", "Hi")              -- positional
greet("Ada", greeting = "Hey")  -- named (any order)
```

The default value (`= expr`) is evaluated at call time. Note `=` here is the **argument** form — general assignment is `let`/`set`, and equality is `==`.

## Recursion

A task may call itself (`factorial` above). There is no special keyword — just `give`.

## Lambdas

A lambda is a value: `(params) => expr`. Store it, pass it, or hand it to a higher-order op:

```synsema
let triple be (n) => n * 3
apply(triple, [1, 2, 3])        -- [3, 6, 9]
where(items, (x) => x.active)   -- inline predicate
```
