Table of Contents
1.07 Procedures, Functions and Subroutines
A LynPlex program may consist of a few statements of code or it may be a very large program broken up into numerous source files.
While it is possible to have a single very large procedure this makes for a unmaintainable program. It is always preferable to break a larger program into separate procedures and functions, each doing a specific thing.
Normally the entry point of the program is the first executable statement in the main part of the program. If there is no executable code, either before, between or after any procedures or functions, the entry point will be taken as the first procedure or function in the program. This can be modified by providing either a single procedure/function call or by using the entry statement.
Procedures and Functions
A procedure is identified by starting with the word procedure(proc) and a function is similarly identified by starting with the word function(func).
In each case they stop with the word end followed by procedure or function.
The essential difference between a procedure and a function is that a function always returns a value. A procedure does not normally return a value although it is possible to do so. In this case the returned value is an integer. By default a procedure always returns a value of zero. Currently there is no way of retrieving the value from outside the procedure.
In functions you either use the name of the function as a target variable to return the value or, alternatively, you can specify the return value in a return statement. If a return statement is specified without a value, the return variable is used. The same applies to procedures, even though currently there is no way of retrieving the value.
Although a function is normally called to return a value, a function may be called as procedure, where the returned value is not important.
Parameters can be passed by value or by reference. By default strings, types and arrays are passed by reference, all other types are passed by value. This may be changed in a procedure or function declaration.
Passing parameters by value means the executor makes a copy of the parameter and this is passed to the procedure or function. The passed variable may be changed by the called procedure or function, but the value in the caller's code will not be changed.
Passing parameters by reference means the executor passes the pointer of variable rather than a copy. The value in caller's code may well be the changed after the call.
A subroutine may be defined within a procedure or function, to execute some code out of line.
A subroutine is identified by starting with the word subroutine(sub) and stopping with the word end followed by subroutine.
A subroutine is invoked by the call statement.
A subroutine may also be defined just by a label. In this case the code must always end with a return statement.
There are no parameters passed to a subroutine. The subroutine will use the same variables as the parent routine.
Nested Procedures and Functions
Procedures and functions may be nested. The nested procedures/functions are visible only inside the parent procedure/function.
The difference between subroutines and nested procedures and functions is that a subroutine shares variables with the parent but a nested procedure or function has its own variables.
Where a global procedure has the same name as a local procedure, the local procedure will be used. There is no way to access a global procedure that has the same name as a local procedure… Then again, why would you want to? …
Local, Static, Shared, External and Common
A variable that is declared (either explicitly or implicitly) and used only within a procedure is local to that procedure and is not the same as any other variable of the same name used elsewhere. The value of a local variable is reset each time the procedure is called.
A variable that is declared as static is also local to the procedure. Unlike a local variable its value does not change across calls to the function or subroutine. When the procedure is called, the value of a static variable will be the same as it was on the previous call.
A variable that is declared as shared in the root segment of a source module is available to all functions and procedures at all levels within the same source module. Unless a variable is explicitly declared as local within a procedure, all references to that variable are taken to refer the shared variable of that name.
Where an object is declared as external it may or may not be defined within the current source module. It must be defined once before use in any of the modules that reference it. Within a source module the external variable can be accessed within any function or procedure in the same way as a shared variable.
If a variable is declared as common, the same variable is available at all levels of the program and across all modules. The use of common is dependent on the linker that is being used. If the linker does not support common storage, then common is effectively the same as shared.
See 2.03 Data Scope for details.
Where a function is very simple it is possible to define it in a single line.
func name[(par1[,...])] = expression
(To be implemented) Functions declared in this form may be treated as macros and included in line to avoid the call overhead.
The USE keyword
This keyword is used on specific commands to passing a user-defined expression.
split str, words, " " use trim
In that example, every element of words will be 'trimmed'.