Table of Contents
Passing Parameters To SUBs And FUNCTIONs
Passing parameters to SUBs and FUNCTIONs usually just works until you find something you don't expect. That is when you need to come to grips with the difference between passing a parameter ByReference and ByValue. This article covers this and points out some other constraints.
When referring to the general issue of ByReference and ByValue those terms will be used. BYREF and BYVAL will be used when referring the new qualifiers in the SUB and FUNCTION Definitions now allowed in Picomite 6.00.02 and planned for the Armmite F4 and and Armmite H7.
Calling a FUNCTION
ret=funname(p1,p2,p3)
Calling a SUB
subname(p1,p2,p3)
Optional method without parenthesis
subname p1,p2,p3
The parenthesis can be omitted and this is how you usually see it. There are some some situations covered below when you must use the parenthesis to get the expected result.
Parameters are passed BYReference by default
All parameters will be passed BYReference by default provided there is nothing that disqualifies the use of BYReference.
This means whatever you do to the parameter's value within the SUB or FUNCTION is reflected in the passed in variable when the FUNCTION or SUB finishes. If you are not expecting this then it can be confusing.
The things that disqualify the use of BYReference and force BYValue are:
1. The type (i.e. INTEGER,FLOAT,STRING, ARRAY) of the passed value and the type in the SUB or FUNCTION Definition are not the same.
2. If a literal value (e.g. 1,1.0,“HELLO”) is passed.
3. The passed value is an expression. e.g. INT(x),LEFT$(S$,2)
4. You tell it you want it passed as BYValue by one of the following methods
This article explains forcing BYValue by enclosing the passed variable in parenthesis
Using the BYVAL qualifier in the later MMBasics that support it.
Note: An Array cannot be passed as BYValue. An Array is always passed as BYReference, you cant change that.
