/ /

XtScript: Function

Learn how to create and use function in XtScript

What is function? Function is a block of codes which have it's own name and arguments passed in it and can be reused over and over again. Creating a function can save your time in coding, that's also apply in XtScript of course.

Function in XtScript are declared using function command followed by function name and arguments to be passed (optional) and closed with endfunction. Each arguments must be separated using a semicolon ;.

To call a function, use call command followed by the function name.

XtScript function basic syntax:

<!--parser:xtscript-->
	function function_name $argument; $argument_2 ...
		 # Your codes for this function goes here
	endfunction

	# To call a function, just use "call" command followed by the function name
	call function_name
<!--/parser:xtscript-->

Learn XtScript Function from examples

Create a simple function with $name argument and call it twice with different arguments value passed each call.

- Code:
<!--parser:xtscript-->
	# Define a function
	function MyName $name
		print <p> My Name is $name </p>
	endfunction

	# Call the function with passing "Adam" as the $name argument value
	call MyName $name=Adam

	# Call the function with passing "Gabriel" as the $name argument value
	call MyName $name=Gabriel
<!--/parser:xtscript-->
- Output:

My Name is Adam

My Name is Gabriel

Accessing XtScript function through xt code

After a function has been defined in the XtScript, we can also call the function outside the script (in xt:code block or code editor mode) through xt function.

Syntax:

<xt:call function="function_name" args="$argument_1=value;$argument_2=value ..." />

A function can be called even outside the XtScript by using xt code.

- Code:
<!--parser:xtscript-->
	# Define a function
	function saving $salary; $spend
		print ($salary - $spend)
	endfunction
<!--/parser:xtscript-->

<!-- Call a function inside HTML codes -->
<p>Last Month, I was able to save <xt:call function="saving" args="$salary=100;$spend=75" /> dollars into my account.</p>
<p>This Month, I was able to save <xt:call function="saving" args="$salary=150;$spend=100" /> dollars into my account.</p>
- Output:

Last Month, I was able to save 25 dollars into my account.

This Month, I was able to save 50 dollars into my account.


pacman, rainbows, and roller s