Using Funlists

Individual Functions can be Run alone but much more can be done if you learn how to use Function Lists. A Funlist is a container for Functions and/or other Funlists, you can nest Funlists to a depth of 32 levels.

Draw Circle (FunList)
    0 MakeLIm: im=NewImage(100,100)'
	(Fun Properties 0)
	0 Name "im" 
	1 Size w,h "100" "100" 
	2 Colour "0.0" "0.0" "1.0" "1.0" "<-  |  -  |  +  |  ->" 
	3 Colourspace "23 sRGB" 
	4 Storage Class "1 Direct" 
	5 Alpha Traits "0 Undefined" 
	6 User Channels "0" 
	7 Create Options "0 Temp." 
    1 MakeRIm: drawing=DrawImage(#_L)
	(Fun Properties 0)
	0 Name "drawing" 
	1 Chain=_false 
	2 Create Options "2 with No Handle" 
        Drawing Details
		(Arg Properties 1)
		fill red
		circle 50,50 75,50

The Funlist above will draw the Image shown, it is called "Draw Circle" and it contains two Functions. Function 0 creates a blue image on the Left and Function 1 draws on this Image creating a new Image on the Right. The numbered lines under each Function heading are the Functions Properties as shown on its Properties Dialog. Note that the DrawImage function has a child Argument in this case it is a script that describes what to draw. Most of the Property lines above are the default values this is usually the case.

Object Lifetimes Notice the ' symbol in Function 0 above, it indicates that the Image that this function creates is Temporary as you can see from Property line 7 of this function. Temporary items will be destroyed when the Funlist in which they were created comes to an end. Function 1 has no such Property Line set and therefore the Right Image remains when Running stops.

To add Funlists and Functions press the Menu Command Button and choose from the Menu. There are families of related functions with menu entries that start with a single letter, on a PC pressing the corresponding key is a shortcut to a submwnu. Many items on the R Menu are used to alter the order in which the contents of a Funlist run.

Infinite Loop (FunList)
    0 LoopIf(_true)
	(Fun Properties 0)
	0 Condition "_true" 

As the name implies the list above will never end of its own accord if you Run it, you will need to stop it. When you press the Run Command Button a special Dialog is shown called the Running Dialog, on it is a Quit button which is used to stop Running when the current function comes to an end.

Conditions On the R menu are several Functions with If in their names, they have a Condition property, if the condition is _true the Function Runs normally if _false the function will be skipped. False is always zero and any other value is true.

Wait (FunList)
    0 SetRunningText(${_time})
	(Fun Properties 0)
	0 Expand=_true 
	1 Message "${_time}" 
    1 LoopIf(_time<3)
	(Fun Properties 0)
	0 Condition "_time<3" 

The Wait Funlist above has two child Functions, one that prints the value of the System Variable _time, and one that causes the Funlist to restart or loop until its value is greater than or equal to 3. _time is set to 0.0 when you press the Run Command Button, then its value is updated after each Functioin is Run, its value is in Seconds. < above is a Comparison operator it compares what is on the left with what is on the right and returns _true or _false. Other Comparison operators are == (equal), > (greater than), >= (greater than or equal), <= (less than or equal) and != (not equal). You can combine two or more Conditional Expressions like _time<3 to make a Compound Conditioinal Expression with the && (and) and || (or) Logic Operators.

Jump (FunList)
    0 GotoIf(_true)-->Skip
	(Fun Properties 0)
	0 Destination "Skip" 
	1 Condition "_true" 
    1 EndIf(_true)
	(Fun Properties 0)
	0 Condition "_true" 
    2 Skip (FunList)
    3 _ok=StringMessage(After Endif)
	(Fun Properties 0)
	0 Expand=_true 
	1 Message "After Endif" 

In the Jump Funlist above the EndIf will never be Run because GotoIf jumps over it, if it were to Run Running would stop and the message would not be printed by Function 3. You can Go backwards or forwards to any Funlist with GotoIf, the destination Funlist can even be empty.

Sum (FunList)
    0 MakeVar: sum=0
	(Fun Properties 0)
	0 Name "sum" 
	1 Value "0" 
	2 Temporary=_false 
    1 MakeVar: x=0(')
	(Fun Properties 0)
	0 Name "x" 
	1 Value "0" 
	2 Temporary=_true 
    2 Loop (FunList)
        0 Eval: x=x+1, sum=sum+x
		(Fun Properties 0)
		0 Setup "x=x+1, sum=sum+x" 
		1 Repeat "" 
        1 LeaveIf(x==10)
		(Fun Properties 0)
		0 Condition "x==10" 
        2 LoopIf(_true)
		(Fun Properties 0)
		0 Condition "_true" 

The first two functions in Sum create two new variables sum=0 and x=0, x is temporary. The child Funlist Loop will run 10 times, LeaveIf will cause it to end when x==10. Eval will evaluate mathematical expressions which are seperated by commas, its Repeat capability is not used here.

Call (FunList)
    0 MakeVar: x=0
	(Fun Properties 0)
	0 Name "x" 
	1 Value "0" 
	2 Temporary=_false 
    1 MakeVar: sum=0
	(Fun Properties 0)
	0 Name "sum" 
	1 Value "0" 
	2 Temporary=_false 
    2 CallIf(x<10)-->Add
	(Fun Properties 0)
	0 Destination "Add" 
	1 Condition "x<10" 
    3 EndIf(x==10)
	(Fun Properties 0)
	0 Condition "x==10" 
    4 LoopIf(_true)
	(Fun Properties 0)
	0 Condition "_true" 
    5 Add (FunList)
        0 Eval: x=x+1,sum=sum+x
		(Fun Properties 0)
		0 Setup "x=x+1, sum=sum+x" 
		1 Repeat "" 
        1 ReturnIf(_true)
		(Fun Properties 0)
		0 Condition "_true" 

The Call Funlist performs the same function as Sum above it, but it illustrates how to use CallIf and ReturnIf, tis is not a very efficient way of doing this. CallIf will jump to the specified Funlist if its condition is _true, it will also remember the location of the next Function so that when ReturnIf Runs execution will jump back to the remembered Function.

Variable (FunList)
    0 MakeVar: sum=0
	(Fun Properties 0)
	0 Name "sum" 
	1 Value "0" 
	2 Temporary=_false 
    1 CallVariable(x,1,1,10)'-->Add
	(Fun Properties 0)
	0 Destination "Add" 
	1 Variable "x" 
	2 Start/Inc/Limit "1" "1" "10" 
	3 Temporary=_true 
    2 Add (FunList)
        0 Eval: sum=sum+x
		(Fun Properties 0)
		0 Setup "sum=sum+x" 
		1 Repeat "" 
        1 ContinueIf(x<10)
		(Fun Properties 0)
		0 Condition "x<10" 

The same functionality again, a bit more efficient. CallVariable repeatedly calls the Add Funlist first creating/setting a variable then before each repeat call it adds an incrament to a the variable in this case x. In the Add child Funlist ContinueIf causes execution to return to CallVariable, if we had used ReturnIf execution would return to the Function after CallVariable causing early termination. CallList works in a similar way to CallVariable it selects each item in a List in turn before Calling the Funlist, you still use ContinueIf to procede to the next item or ReturnIf to finish early.

Eval (FunList)
    0 MakeVar: sum=0
	(Fun Properties 0)
	0 Name "sum" 
	1 Value "0" 
	2 Temporary=_false 
    1 Eval: _t=10, x=0 Repeat{x=x+1, sum=sum+x}
	(Fun Properties 0)
	0 Setup "_t=10, x=0" 
	1 Repeat "x=x+1, sum=sum+x" 

Finaly the almost best solution using the Eval Functions Repeat property. The System Variable _t counts down, when it reaches zero the Function ends.

MakeVar: sum=0.5*10*11
	(Fun Properties 0)
	0 Name "sum" 
	1 Value "0.5*10*11" 
	2 Temporary=_false 

Is the best, it uses the equation sum=0.5*n*(n+1). This is a single Function without a Funlist.