Tinderbox v9 Icon

Additional examples

Important note: all examples follow aTbRef naming conventions.


Basic example

A simple function might be written to return a number with an 18% tax uplift applied to the input number:

function fAddTax(iPrice){return(1.18*iPrice);}; 

or, split out only lines for clarity:

	function fAddTax(iPrice){
		return(1.18*iPrice);
	};
	

Indeed, the function can be unpacked further, and comments inserted, as shown below for further clarity, as:

	function fAddTax(iPrice){	 // Arguments: iPrice - a number, the starting price
	 //
	 // make a variable (expected type of Number) and set it to argument #1
	 var:number vNum = iPrice;
	 // multiply variable by tax rate
	 vNum = 1.18 * vNum;
	 //return the tax-adjusted price
	 return vNum;
	};

This defines a new function named 'fAddTax' that can be used in any action or expression (including ^action()^ in templates) to add 18% tax to an input figure. It has one input argument, and returns a single value. Thus:

$MyNumber = fAddTax(500); 

would set MyNumber's value to 1.18*500, i.e. 590.

But hard-coding (i.e. 1,18) values like tax rates is generally considered bad practice as values can (will!) change. A better function might be this, comments, line breaks for for clarity:

	function fAddTax(iPrice){	 // Arguments: iPrice - a number, the starting price
	 //
	// var"number vTaxRate = 1.1.18;
	 // make a variable (expected type of Number) and set it to argument #1
	 var:number vNum = iPrice;
	 // multiply variable by tax rate
	 vNum = vTaxRate * vNum;
	 //return the tax-adjusted price
	 return vNum;
	};

But, supposedly fixed values like tax rates can change when governments change. So…

Adding an additional function argument

What if the tax rate changes? This can be accommodated by adding a second argument for the tax rate. Extra comments, line breaks for for clarity:

	function fAddVarTax(iPrice,iTaxRate){
		// Arguments: 
		//   iPrice - a number, the starting price
		//   iTaxRate - tax rate percentage as a number (15 not 0.15)
		// --------------
		
		// make a variable (expected type of Number) and set it to argument #1
		var:number vNum = iPrice;
		// multiply variable by tax rate
		var:number vTax = iTaxRate;
		vTax = 1 + (vTax/100);
		vNum = vTax * vNum;
		//return the tax-adjusted price
		return vNum;
	};

Thus:

$MyNumber = fAddVarTax(500,20); 

would pass a 20% tax rate, set MyNumber's value to 1.20*500, i.e. 600.

Further examples:

	function fAnswer() {
		return 42;
	}

always returns 42. Or:

	function fFibonacci(iNum){
		if(iNum<2){
			return (iNum);
		};
		return fFibonacci(iNum-1)+fFibonacci(iNum-2);
	}

returns the iNumth Fibonacci number.

Using more concise code

For the experts who understand the flow of the code, the function defined above could equally well be defined in a terser form:

	function fVarTax2(N,T){
		return ((1+(T/100))*N);
	};

Those with coding expertise my prefer this form. However, aTbRef code examples here are written assuming a reader with little or no coding expertise, thus the use of a more explicit and verbose format.


Next: Additional examples