Using subroutines


Call

Subroutine

Return

Subroutines are meant to be a group of objects which are called from multiple points in the program and return execution to the point where they were called from. There are three objects which provide the functionality of subroutines: A "Subroutine" object and a "Return" object which define beginning and end of a subroutine and a "Call" object which is used to execute the subroutine. Figure 1 shows all three subroutine objects.
Fig. 1: Subroutine objects

Defining a subroutine

For defining a subroutine insert at first a "Subroutine" object. In the property dialog specify the subroutine name and the input variables. These variables appear as input variables of the appropriate "Call" objects and as output variables of the "Subroutine" object. At the end of your subroutine insert a "Return" object. In the property dialog at "Belongs to" choose the "Subroutine" object the "Return" object belogs to. Then specify the variable that contain the return values of the subroutine. These variables appear as input variables of the "Return" object and as output variables of the appropriate "Call" objects.

During execution "Return" objects have the following special behaviour: If executed the "Return" objects wait for all parallel executing threads of a subrouting to finish. In Figure 1 this means that the subroutine does not return until the object "Do Something Parallel" has finished. If the "Return" object is called when executing a non parallel sub branch, the sub branch does not return to the point it originated but the subroutine returns immediately.

Calling a subroutine

Insert a "Call" object into the workspace. Within the property dialog specify at "Called routine" the name of the subroutine which should be called. This routine can be on any workspace panel. After that press "Update Variables" to synchronize the "Call" object's input and output variables with the variables of the subroutine. If you change input and output variables of a subroutine, you need to update all "Call" objects calling the subroutine. This is not done automatically for security reasons.
Fig. 2: Recursive calling

Recursive calling of a subroutine is explicitly allowed. Figure 2 shows an object layout that can be used to recursively calculate a factorial number, although this is quite inefficient in this case as a new thread tree is created on each recursion.


The Vimms User Manual