blob: 62ce0b0e085ce2a7faa4ae78ad932f92d48bcf2f [file] [log] [blame]
<!doctype html>
<!-- Generated by FreeMarker/Docgen from DocBook -->
<html lang="en" class="page-type-section">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">
<title>function, return - FreeMarker Manual</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="FreeMarker Manual">
<meta property="og:title" content="function, return">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/ref_directive_function.html">
<link rel="canonical" href="http://example.com/ref_directive_function.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338519184">
</head>
<body itemscope itemtype="https://schema.org/Code">
<meta itemprop="url" content="http://example.com/">
<meta itemprop="name" content="FreeMarker Manual">
<!--[if lte IE 9]>
<div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div>
<![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo">
</a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref.html"><span itemprop="name">Reference</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_directives.html"><span itemprop="name">Directive Reference</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="ref_directive_function.html"><span itemprop="name">function, return</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li><li><a href="api/index.html">API</a></li><li><a href="../index.html">Home</a></li></ul></div></div></div> <div class="main-content site-width">
<div class="content-wrapper">
<div id="table-of-contents-wrapper" class="col-left">
<script>var breadcrumb = ["FreeMarker Manual","Reference","Directive Reference","function, return"];</script>
<script src="toc.js?1594338519184"></script>
<script src="docgen-resources/main.min.js?1594338519184"></script>
</div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="ref_directive_macro.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_directive_flush.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="ref_directive_function" itemprop="headline">function, return</h1>
</div></div><div class="page-menu">
<div class="page-menu-title">Page Contents</div>
<ul><li><a class="page-menu-link" href="#autoid_108" data-menu-target="autoid_108">Synopsis</a></li><li><a class="page-menu-link" href="#autoid_109" data-menu-target="autoid_109">Description</a></li></ul> </div><a name="ref.directive.function"></a><a name="ref.directive.function.return"></a>
<h2 class="content-header header-section2" id="autoid_108">Synopsis</h2>
<pre class="metaTemplate">
<code class="inline-code">&lt;#function <em class="code-color">name</em> <em class="code-color">param1</em> <em class="code-color">param2</em> <em class="code-color">... paramN</em>&gt;
<em class="code-color">...</em>
&lt;#return <em class="code-color">returnValue</em>&gt;
<em class="code-color">...</em>
&lt;/#function&gt;</code>
</pre>
<p>Where:</p>
<ul>
<li>
<code class="inline-code"><em class="code-color">name</em></code>: name
of method variable (not expression)
</li>
<li>
<code class="inline-code"><em class="code-color">param1</em></code>,
<code class="inline-code"><em class="code-color">param2</em></code>, ...etc.:
the name of the <a href="dgui_misc_var.html">local
variables</a> store the parameter values (not expression),
optionally followed by <code class="inline-code">=</code> and the default
value (that&#39;s an expression).
</li>
<li>
<code class="inline-code"><em class="code-color">paramN</em></code>, the
last parameter, may optionally include a trailing ellipsis
(<code class="inline-code">...</code>), which indicates the macro takes a
variable number of parameters. Local variable
<code class="inline-code"><em class="code-color">paramN</em></code> will be a
sequence of the extra parameters.
</li>
<li>
<code class="inline-code"><em class="code-color">returnValue</em></code>:
the expression that calculates the value of the method
call.
</li>
</ul>
<p>The <code class="inline-code">return</code> directive can be used anywhere
and for any times between the <code class="inline-code">&lt;#function
<em class="code-color">...</em>&gt;</code> and
<code class="inline-code">&lt;/#function&gt;</code>.</p>
<p>Parameters without default value must precede parameters with
default value
(<code class="inline-code"><em class="code-color">paramName</em>=<em class="code-color">defaultValue</em></code>).</p>
<h2 class="content-header header-section2" id="autoid_109">Description</h2>
<p>Creates a method variable (in the current namespace, if you
know namespace feature). This directive works in the same way as the
<a href="ref_directive_macro.html#ref.directive.macro"><code>macro</code>
directive</a>, except that <code class="inline-code">return</code> directive
<em>must</em> have a parameter that specifies the return
value of the method, and that attempts to write to the output will
be ignored. If the <code class="inline-code">&lt;/#function&gt;</code> is reached
(i.e. there was no <code class="inline-code">return
<em class="code-color">returnValue</em></code>), then the return
value of the method is an undefined variable.</p>
<p>Example 1: Creating a method that calculates the average of
two numbers:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#function avg x y&gt;
&lt;#return (x + y) / 2&gt;
&lt;/#function&gt;
${avg(10, 20)}</pre></div>
<p>will print:</p>
<div class="code-wrapper"><pre class="code-block code-output">15</pre></div>
<p>Example 2: Creating a method that calculates the average of
multiple numbers:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#function avg nums...&gt;
&lt;#local sum = 0&gt;
&lt;#list nums as num&gt;
&lt;#local sum = sum + num&gt;
&lt;/#list&gt;
&lt;#if nums?size != 0&gt;
&lt;#return sum / nums?size&gt;
&lt;/#if&gt;
&lt;/#function&gt;
${avg(10, 20)}
${avg(10, 20, 30, 40)}
${avg()!&quot;N/A&quot;}</pre></div>
<p>will print:</p>
<div class="code-wrapper"><pre class="code-block code-output">15
25
N/A</pre></div>
<div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="ref_directive_macro.html"><span>Previous</span></a><a class="paging-arrow next" href="ref_directive_flush.html"><span>Next</span></a></div></div></div></div> </div>
</div>
<div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"></div><div class="col-right"><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"> <p class="last-generated">
Last generated:
<time itemprop="dateModified" datetime="2020-07-09T23:48:39Z" title="Thursday, July 9, 2020 11:48:39 PM GMT">2020-07-09 23:48:39 GMT</time> </p>
<p class="copyright">
© <span itemprop="copyrightYear">1999</span>–2020
<a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p>
</div></div></div></body>
</html>