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.
.function {helloworld}
Hello, world!
.helloworldHello, world!
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.
.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}
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.
.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.
.function {greet}
to from?:
Hello, .to from .from::otherwise {unnamed}!
.greet {world}
.greet {world} {John}Hello, world from unnamed!
Hello, world from John!
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.
.function {myexample}
title content:
.box {.title}
.content
.myexample {Example title}
This is the content of the example.This is the content of the example.
In Quarkdown, there are no return statements. Every reached instruction becomes part of the output – see Conditional statements.
.function {myfunction}
.if {.iseven {3}}
A
B
.myfunctionB
Functions can return any Markdown content.
.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.
.function {area}
width height:
.multiply {.width} by:{.height}
The area of the rectangle is **.area {4} {2}**.The area of the rectangle is 8.
.function {isadult}
age:
.age::isgreater than:{18}
.if {.isadult age:{20}}
You're an adult!You’re an adult!