.docname {Declaring functions}
.include {docs}

You can declare functions using the **`.function`** .docslink {quarkdown-stdlib/com.quarkdown.stdlib.module.Flow/function.html} function (in Quarkdown, everything is a function!).

It accepts two arguments: the function name and its body. The function can then be invoked as a normal function call - see [Syntax of a function call](syntax-of-a-function-call.qd).

.examplemirror
    .function {helloworld}
        Hello, world!

    .helloworld

## Parameters

The body parameter is a [lambda](lambda.qd), and each parameter of the function is a parameter of the lambda block. You can access each argument within the function body as a [variable](variables.qd), which in Quarkdown is essentially a function with no parameters.

.examplemirror
    .function {greet}
        to from:
        Hello, .to from .from!

    .greet {world} {John}

> Tip: You can name arguments to improve readability:
>
> ```markdown
> .greet {world} from:{John}
> ```

### Optional parameters

If a function parameter ends with a question mark `?`, it becomes optional. When you do not provide the corresponding argument, it receives the value [`None`](none.qd).

.examplemirror
    .function {greet}
        to from?:
        Hello, .to from .from!

    .greet {world}

    .greet {world} {John}

`None` provides several useful [operations](none.qd#operations), such as `.otherwise` for placeholders that emulate default parameter values.

.examplemirror
    .function {greet}
        to from?:
        Hello, .to from .from::otherwise {unnamed}!

    .greet {world}

    .greet {world} {John}

### Block parameters

Block arguments always correspond to the last parameter of a function. There is no special syntax to declare them; just define the function as usual.

.examplemirror
    .function {myexample}
        title content:
        .box {.title}
            .content

    .myexample {Example title}
        This is the content of the example.

## Returning values

In Quarkdown, there are no return statements. Every reached instruction becomes part of the output - see [Conditional statements](conditional-statements.qd).

.examplemirror
    .function {myfunction}
        .if {.iseven {3}}
            A
        B

    .myfunction

Functions can return any Markdown content. 

.examplemirror
    .function {greet}
        to from:
        **Hello, .to** from .from!

    .greet {world} from:{John}

Quarkdown is [weakly typed](typing.qd), so functions can return any type of value.

.examplemirror
    .function {area}
        width height:
        .multiply {.width} by:{.height}

    The area of the rectangle is **.area {4} {2}**.

.examplemirror
    .function {isadult}
        age:
        .age::isgreater than:{18}

    .if {.isadult age:{20}}
        You're an adult!

## Overwriting functions

A function can be redeclared at any point, and the new definition takes precedence over the previous one from that point onward within that scope.

.exampleoutput output:{
    QUARKDOWN

    quarkdown
}
    .uppercase {Quarkdown}

    .function {uppercase}
        text:
        .text::lowercase

    .uppercase {Quarkdown}

This behavior can be disabled at the command-line level via the [`--forbid-function-overwriting`](cli-compiler.qd#function-overwriting) option, which raises a compilation error when a function is declared with a name that is already in use.
