# func

The function class for Cheddar

## Arguments:

| name        | type     | description                                                                                                              |
| ----------- | -------- | ------------------------------------------------------------------------------------------------------------------------ |
| `arguments` | 2D array | (described below) this matrix described the arguments for the function. In the form of `[ ["arg_name", { <options> }] ]` |
| `body`      | function | (described below) the function body                                                                                      |

#### arguments matrix:

| name       | type              | default | description                                                                                         |
| ---------- | ----------------- | ------- | --------------------------------------------------------------------------------------------------- |
| `Type`     | `cheddar.class`   | n/a     | If provided, the argument is required to be of the given type                                       |
| `Default`  | `cheddar.class{}` | n/a     | If provided and the argument is not provided, it will be set to this rather than throwing an error. |
| `Splat`    | bool              | `false` | If provided, this and the following arguments will be combined into a `cheddar.array` object.       |
| `Optional` | bool              | `false` | If true, the argument will be `nil` if not provided                                                 |

#### function body:

| argument | description                                                                 |
| -------- | --------------------------------------------------------------------------- |
| first    | This is the **function's scope** as a `cheddar.scope`.                      |
| second   | This is a function which returns a variable's value, given a variable name. |

## Usage:

`-> (a, b = 0) a + b`, with api:

```javascript
new cheddar.func(
  [
    ["a", {}],
    ["b", {
        Default: cheddar.init(
          cheddar.number,
          10, 0, 0
        )
    }]
  ],
  function(scope, input) {
    return cheddar.init(
      cheddar.number,
      10, 0,
      input("a").value + input("b").value
    );
  }
)
```

## Internal Usage

### Execution:

```javascript
func.exec([
    arg1, arg2, ...
], scope)
```

`scope` will be set to `self` variable, if none, set to `null`.

### Arguments:

```javascript
func.args
```

the 2D matrix representing the arguments the function is requesting.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cheddar.vihan.org/docs/developing/api/primitives/func.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
