/

Declaring functions

You can declare functions using the .function docs ↗ 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.

Example 1

.function {helloworld}
    Hello, world!

.helloworld

Hello, world!

Parameters

The body parameter is a lambda, 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, which in Quarkdown is essentially a function with no parameters.

Example 2

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

.greet {world} {John}

Hello, world from John!

You can name arguments to improve readability:

.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.

Example 3

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

.greet {world}

.greet {world} {John}

Hello, world from None!

Hello, world from John!

None provides several useful operations, such as .otherwise for placeholders that emulate default parameter values.

Example 4

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

.greet {world}

.greet {world} {John}

Hello, world from unnamed!

Hello, world from 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.

Example 5

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

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

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.

Example 6

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

.myfunction

B

Functions can return any Markdown content.

Example 7

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

.greet {world} from:{John}

Hello, world from John!

Quarkdown is weakly typed, so functions can return any type of value.

Example 8

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

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

The area of the rectangle is 8.

Example 9

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

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

You’re an adult!