home download demos platforms restrictions help

 

What are high level user defined functions and why are these prefered to 'gosub' and 'call' statements?

User defined high level functions are similar to subroutines in that they allow small sections of code to be packaged into self contained components. However they differ in that they allow a kind of virtual fence to be built around a subroutine with an entry and exit gate. This prevents other sections of code arbitarilly jumping into a subroutine at any random point. This ensures that the subroutine can only be executed as intended.

Futhermore, user defined high level functions allow variables to be defined as local to the function. These variables are treated as private to the function and cannot be accessed from outside the function.

This means that the programmer does not need to worry about his function accidently overwriting a variable used elsewhere within the program or about some other piece of code accidently overwriting a variable used by the function.

User defined high level functions also provide a private parameter passing mechanism which ordinary subroutines do not provide. Ordinary subroutines normally pass data into and out of the subroutine by using global variables. User defined high level functions on the otherhand use variables that are local to the function. This means that the user of the function does not need to worry about which global variables are used to to pass data into and out of a function. The function is invoked by the user with a set of actual parameters and these parameters are used to populate the local variables specified by the definition of the function. The function sees all the appropriote local variables initialised before it starts to execute.

The XCSB compiler is able to ensure that all parameters required by a user defined high level function are provided when the function is invoked.

In contrast ordinary subroutines do not guarantee the order in which code is executed (because they can be entered anywhere), they do not have private variables (because all variables are visible globally), they do not have anonymous viariables through which parameters can be passed (meaning that globals must be used for the purpose) AND there is no way to audit parameter passing (a parameter passed though a global might get missed by the user and other PIC BASIC compilers that do not support user defined high level functions would not be able to detect this)

The XCSB compiler optimises local variable and parameter space used. It organises the layout of local variables and parameters so that the same space can be used for multiple functions where the compiler determines that the space can be used exclusively by any function at any given time. This is not as simple a task as it might first appear since functions can call other functions and other tasks (and interrupt handlers) might also be active and need to be taken into account.

The XCSB compiler is able to optimise functions based on the parameters used to invoke them (e.g. if a parameter is always a specific constant then the overhead of passing that parameter is reduced by embedding the constant in the function and not passing the parameter at all - which could have a further optimising effect if the constant (parameter) is used in a conditional expression or loop)


 
home download demos platforms restrictions help