{"route":"/en-US-v0.13.1/reference/foundations/function/","title":"Function","description":"Documentation for the Function type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"element-functions","name":"Element functions","children":[]},{"id":"function-scopes","name":"Function scopes","children":[]},{"id":"defining-functions","name":"Defining functions","children":[]},{"id":"importing-functions","name":"Importing functions","children":[]},{"id":"unnamed","name":"Unnamed","children":[]},{"id":"note-on-function-purity","name":"Note on function purity","children":[]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-with","name":"With","children":[{"id":"definitions-with-arguments","name":"arguments","children":[]}]},{"id":"definitions-where","name":"Where","children":[{"id":"definitions-where-fields","name":"fields","children":[]}]}]}],"body":{"kind":"type","content":{"name":"function","title":"Function","keywords":[],"oneliner":"A mapping from argument values to a return value.","details":"<p>A mapping from argument values to a return value.</p>\n<p>You can call a function by writing a comma-separated list of function\n<em>arguments</em> enclosed in parentheses directly after the function name.\nAdditionally, you can pass any number of trailing content blocks arguments\nto a function <em>after</em> the normal argument list. If the normal argument list\nwould become empty, it can be omitted. Typst supports positional and named\narguments. The former are identified by position and type, while the latter\nare written as <code>name: value</code>.</p>\n<p>Within math mode, function calls have special behaviour. See the\n<a href=\"/en-US-v0.13.1/reference/math/\">math documentation</a> for more details.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-comment\">// Call a function.</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">list</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">[</span>A<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span> <span class=\"typ-punct\">[</span>B<span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-comment\">// Named arguments and trailing</span>\n<span class=\"typ-comment\">// content blocks.</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">enum</span><span class=\"typ-punct\">(</span>start<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>A<span class=\"typ-punct\">]</span><span class=\"typ-punct\">[</span>B<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-comment\">// Version without parentheses.</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">list</span><span class=\"typ-punct\">[</span>A<span class=\"typ-punct\">]</span><span class=\"typ-punct\">[</span>B<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/87cba5b2546c4d8134e4fbb8ab2e42e8.png\" alt=\"Preview\"></div></div>\n<p>Functions are a fundamental building block of Typst. Typst provides\nfunctions for a variety of typesetting tasks. Moreover, the markup you write\nis backed by functions and all styling happens through functions. This\nreference lists all available functions and how you can use them. Please\nalso refer to the documentation about <a href=\"/en-US-v0.13.1/reference/styling/#set-rules\">set</a> and\n<a href=\"/en-US-v0.13.1/reference/styling/#show-rules\">show</a> rules to learn about additional ways you can\nwork with functions in Typst.</p>\n<h2 id=\"element-functions\">Element functions</h2>\n<p>Some functions are associated with <em>elements</em> like <a href=\"/en-US-v0.13.1/reference/model/heading/\">headings</a> or\n<a href=\"/en-US-v0.13.1/reference/model/table/\">tables</a>. When called, these create an element of their respective\nkind. In contrast to normal functions, they can further be used in <a href=\"/en-US-v0.13.1/reference/styling/#set-rules\">set\nrules</a>, <a href=\"/en-US-v0.13.1/reference/styling/#show-rules\">show rules</a>, and\n<a href=\"/en-US-v0.13.1/reference/foundations/selector/\">selectors</a>.</p>\n<h2 id=\"function-scopes\">Function scopes</h2>\n<p>Functions can hold related definitions in their own scope, similar to a\n<a href=\"/en-US-v0.13.1/reference/scripting/#modules\">module</a>. Examples of this are\n<a href=\"/en-US-v0.13.1/reference/foundations/assert/#definitions-eq\"><code>assert.eq</code></a> or <a href=\"/en-US-v0.13.1/reference/model/list/#definitions-item\"><code>list.item</code></a>. However, this\nfeature is currently only available for built-in functions.</p>\n<h2 id=\"defining-functions\">Defining functions</h2>\n<p>You can define your own function with a <a href=\"/en-US-v0.13.1/reference/scripting/#bindings\">let binding</a>\nthat has a parameter list after the binding's name. The parameter list can\ncontain mandatory positional parameters, named parameters with default\nvalues and <a href=\"/en-US-v0.13.1/reference/foundations/arguments/\">argument sinks</a>.</p>\n<p>The right-hand side of a function binding is the function body, which can be\na block or any other expression. It defines the function's return value and\ncan depend on the parameters. If the function body is a <a href=\"/en-US-v0.13.1/reference/scripting/#blocks\">code\nblock</a>, the return value is the result of joining the\nvalues of each expression in the block.</p>\n<p>Within a function body, the <code>return</code> keyword can be used to exit early and\noptionally specify a return value. If no explicit return value is given, the\nbody evaluates to the result of joining all expressions preceding the\n<code>return</code>.</p>\n<p>Functions that don't return any meaningful value return <a href=\"/en-US-v0.13.1/reference/foundations/none/\" title=\"`none`\"><code>none</code></a> instead.\nThe return type of such functions is not explicitly specified in the\ndocumentation. (An example of this is <a href=\"/en-US-v0.13.1/reference/foundations/array/#definitions-push\" title=\"`array.push`\"><code>array.push</code></a>).</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">alert</span><span class=\"typ-punct\">(</span>body<span class=\"typ-punct\">,</span> fill<span class=\"typ-punct\">:</span> red<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>white<span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">set</span> <span class=\"typ-func\">align</span><span class=\"typ-punct\">(</span>center<span class=\"typ-punct\">)</span>\n  <span class=\"typ-func\">rect</span><span class=\"typ-punct\">(</span>\n    fill<span class=\"typ-punct\">:</span> fill<span class=\"typ-punct\">,</span>\n    inset<span class=\"typ-punct\">:</span> <span class=\"typ-num\">8pt</span><span class=\"typ-punct\">,</span>\n    radius<span class=\"typ-punct\">:</span> <span class=\"typ-num\">4pt</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">[</span><span class=\"typ-strong\">*Warning:<span class=\"typ-escape\">\\</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">body</span>*</span><span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">alert</span><span class=\"typ-punct\">[</span>\n  Danger is imminent!\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">alert</span><span class=\"typ-punct\">(</span>fill<span class=\"typ-punct\">:</span> blue<span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>\n  KEEP OFF TRACKS\n<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/e7ac0ae1342dcd1b7bfc1dce40ec5bed.png\" alt=\"Preview\"></div></div>\n<h2 id=\"importing-functions\">Importing functions</h2>\n<p>Functions can be imported from one file (<a href=\"/en-US-v0.13.1/reference/scripting/#modules\"><code>module</code></a>) into\nanother using <code><span class=\"typ-key\">import</span></code>. For example, assume that we have defined the <code>alert</code>\nfunction from the previous example in a file called <code>foo.typ</code>. We can import\nit into another file by writing <code><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;foo.typ&quot;</span><span class=\"typ-punct\">:</span> alert</code>.</p>\n<h2 id=\"unnamed\">Unnamed functions</h2>\n<p>You can also created an unnamed function without creating a binding by\nspecifying a parameter list followed by <code>=&gt;</code> and the function body. If your\nfunction has just one parameter, the parentheses around the parameter list\nare optional. Unnamed functions are mainly useful for show rules, but also\nfor settable properties that take functions like the page function's\n<a href=\"/en-US-v0.13.1/reference/layout/page/#parameters-footer\"><code>footer</code></a> property.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-str\">&quot;once?&quot;</span><span class=\"typ-punct\">:</span> it <span class=\"typ-op\">=&gt;</span> <span class=\"typ-punct\">[</span><span class=\"typ-pol\">#</span><span class=\"typ-pol\">it</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">it</span><span class=\"typ-punct\">]</span>\nonce?\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/c9779efb0fcc717fd4868ec6868bdcf9.png\" alt=\"Preview\"></div></div>\n<h2 id=\"note-on-function-purity\">Note on function purity</h2>\n<p>In Typst, all functions are <em>pure.</em> This means that for the same\narguments, they always return the same result. They cannot &quot;remember&quot; things to\nproduce another value when they are called a second time.</p>\n<p>The only exception are built-in methods like\n<a href=\"/en-US-v0.13.1/reference/foundations/array/#definitions-push\"><code>array.push(value)</code></a>. These can modify the values they are\ncalled on.</p>","constructor":null,"scope":[{"path":["function"],"name":"with","title":"With","keywords":[],"oneliner":"Returns a new function that has the given arguments pre-applied.","element":false,"contextual":false,"deprecation":null,"details":"<p>Returns a new function that has the given arguments pre-applied.</p>","example":null,"self":true,"params":[{"name":"arguments","details":"<p>The arguments to apply to the function.</p>","example":null,"types":["any"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":true,"settable":false}],"returns":["function"],"scope":[]},{"path":["function"],"name":"where","title":"Where","keywords":[],"oneliner":"Returns a selector that filters for elements belonging to this function","element":false,"contextual":false,"deprecation":null,"details":"<p>Returns a selector that filters for elements belonging to this function\nwhose fields have the values of the given arguments.</p>","example":"<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> heading<span class=\"typ-punct\">.</span><span class=\"typ-func\">where</span><span class=\"typ-punct\">(</span>level<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">:</span> <span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>blue<span class=\"typ-punct\">)</span>\n<span class=\"typ-heading\">= Section</span>\n<span class=\"typ-heading\">== Subsection</span>\n<span class=\"typ-heading\">=== Sub-subsection</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/54e4780e95888ad47cba40e40c1d918a.png\" alt=\"Preview\"></div></div>","self":true,"params":[{"name":"fields","details":"<p>The fields to filter for.</p>","example":null,"types":["any"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":true,"settable":false}],"returns":["selector"],"scope":[]}]}}}