XtScript is the Xtgem's own server side scripting language to provide Xtgem user a server side scripting. Just like another languages that derived from PHP, XtScript also capable to use and handle function.

Xtscript tutorial by Weblogwap

What is a Function? Function is a block of codes that has it's own name. Function also have arguments that can be passed to it. By creating a function we can save time to do repeated tasks involving same process with different variables by calling the declared function and pass different arguments on each call to be passed as the variables value.

For me who already knew the basic understanding of function in Javascript, using function in XtScript is quite easy in my opinion, except the "loosy" nature of XtScript where there aren't any characters or sign to distinct variable, argument, and the value but the $ sign.

Function in XtScript is declared using function keyword, followed by function name and arguments to be passed, and closed with endfunction. Arguments are declared with $ prefix and each arguments are separated using ;.

To call a function, just use call command followed by the function name and the arguments to be passed as.

XtScript function basic syntax:

# Define a function
function function_name $argument1; $argument2 ...
# Your codes for this function

# Call the defined function
call function_name $argument1=...; $argument2=...

Function examples in XtScript

Let's create a simple XtScript function to calculate the average of numbers as the argument and then print it as part of a table.

# Define a function
function math_row_table $name; $e1; $e2; $e3
var $avg = ($e1 + $e2 + $e3 / 3)
print <tr><td>$name</td><td>$e1</td><td>$e2</td><td>$e3</td><td>$avg</td></tr>

print <table border="1">
print <tr><th colspan="5">Weblogwap High School Math Exams Report</th></tr>
print <tr><th>Name</th><th>1st term</th><th>2nd term</th><th>3rd term</th><th>Average</th></tr>

# Call functions multiple times and pass different arguments value on each call
call math_row_table $name=John; $e1=70; $e2=65; $e3=80
call math_row_table $name=Stella; $e1=90; $e2=91; $e3=87
call math_row_table $name=Jane; $e1=78; $e2=86; $e3=89

print </table>

Weblogwap High School Math Exams Report
Name1st term2nd term3rd termAverage

We can also assign default value for the arguments when declaring a function in XtScript. When we call the function but we are not passing a value to the arguments, the assigned value when the function was declared will be used instead.

# Define a function and assign default argument value
function user_eats $br=unspecified; $lu=unspecified; $di=unspecified
print Breakfast: $br, Lunch: $lu, Dinner: $di

print <p>- Ali daily dish menu -<br/>
call user_eats $br=Bread; $lu=Pasta; $di=Rice
print </p>

print <p>- Umar daily dish menu -<br/>
# Let's call the function without passing some arguments
call user_eats $di=Soup
print </p>

- Ali daily dish menu -
Breakfast: Bread, Lunch: Pasta, Dinner: Rice

- Umar daily dish menu -
Breakfast: unspecified, Lunch: unspecified, Dinner: Soup

Call a defined xtscript function outside the script using xt:call

After we have define an XtScript function on a page, we also can call the function in the page even outside the script using "xt call" tag method. xt call can be used inside code block if we are editing the page using building tools or can be written directly if we are editing the page using code editor mode.

Basic syntax of xt function to call a defined XtScript function:

<xt:call function="function_name" args="argument1=...;$argument2=..." />

xt:call and XtScript function examples

On the example below, we will define a function in XtScript to check a number and do conditional operator to print different outputs based on the argument value. Then we will call the function outside the xtscript using xt:call and pass different argument value.

# Define a function
function check_age $age
if $age < 18
print He is still too young.
print Oke, he is old enough.

<p>Michael is 17 Years old. <xt:call function="check_age" args="$age=17" /></p>
<p>Adam is 20 Years old. <xt:call function="check_age" args="$age=20" /></p>

Michael is 17 Years old. He is still too young.

Adam is 20 Years old. Oke, he is old enough.