Functions are coded procedures within the script engine. A script can invoke functions, which take arguments, perform a specific action, and return the result. This process might sound similar to a named template, but there are large differences between the two:
  • A named template is actual script code, whereas a function is part of the underlying Junos operating system itself.
  • Values are provided to named templates through the use of parameters, which are assigned by name, but functions use arguments where a precise order is mandated.
  • Functions actually return results, whereas named templates can only write to the result tree and have that result tree fragment redirected to a variable in the calling function.

The syntax of functions differs from that of named templates as well. The call statement is not used; only the function name is provided and the required arguments specified within parenthesis:
expr substring( $localtime, 1, 3 );

Functions return values, as an example the substring() function returns a string. The above code writes the string value to the result tree. The script code could assign the string value to a variable instead using the following syntax:
var $day-string = substring( $localtime, 1, 3); 

Note the difference between assigning a value to a variable from a named template versus from a function:
var $day-of-week = { call get-day-of-week(); }

version 1.0;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

match / {
    <op-script-results> {
        /* get-day-of-week returns the day – assign it to $day-of-week */
        var $day-of-week = { call get-day-of-week(); }
        /* Output day string to the console */
        <output> $day-of-week;
    }
}

/* Extract the day of week string from the $localtime global parameter */
template get-day-of-week {
    /* Write the first three characters of the $localtime to the result tree */
    expr substring( $localtime, 1, 3 );
}