ZBasic Language Reference
81
ZBasic Microcontrollers
BasicX Compatibility Note
The BasicX compiler does not allow direct recursive invocation of
a function but the BasicX mode of the ZBasic compiler does.
3.18 Using Default Parameter Values in Subroutines and Functions
ZBasic supports the designation of default values for parameters to subroutines and functions. This
saves time when typing statements and makes the code easier to read when a particular routine is
usually invoked with one or more parameters having the same constant value. Specification of the
default value is done by adding an equal sign and a constant expression following the type specification
of the formal parameter in the routine definition as illustrated in the example below. In the first call to the
subroutine foo, the second parameter is omitted so the compiler automatically adds the specified default
value for the second parameter.
Sub Main()
Call foo(3)
Call foo(3, 5)
End Sub
Sub foo(ByVal size as Byte, ByVal cnt as Byte = 1)
End Sub
If any parameter has a default value specified, all parameters following that parameter must also have a
default value. Also, only parameters that are passed ByVal may have a default value. Some parameter
types, like arrays and structures for example, are always passed ByRef even if they are defined as ByVal
and therefore cannot have a default value specification.
3.19 Subroutine and Function Overloads
Overloading is the computer science term for defining two or more procedures having the same name but
having different formal parameter lists. The compiler selects which of the overloads to invoke based on
the actual parameters specified in a particular call. Several of the ZBasic System Library procedures
have multiple overloads (e.g. GetADC()) and you may optionally defined overloads in your own
application code.
Normally, the compiler will disallow the definition of multiple subroutines/functions with the same name. If
you wish to use this capability in your application, you must specifically enable it. This can be achieved
by using the directive Option Overload in the first module compiled. Alternately, the compiler option
-overload has the same effect; it must occur on the command line or in the .pjt file before the first .bas
file processed. It is important to note that subroutine/function overloading is automatically enabled if
ZBasic object-oriented extensions are enabled. See Chapter 4 for more information on the object-
oriented extensions.
As mentioned earlier, the compiler determines which of several identically named procedures to invoke
depending on the number and types of the parameters given in each particular instance. The procedure
name along with the types and passing method of the parameters (if any) is referred to as the procedures
signature. If the compiler cannot find a procedure with a signature compatible with a particular
invocation, it will generate an error message to that effect. Likewise, if there is more than one procedure
whose signature is compatible with a particular invocation, the compiler will issue an error message
indicating an ambiguous refererence and enumerating the possible matches.
It is important to note that the fact that a procedure is a subroutine or a function is not part of the
signature. Moreover, a functions return type is not part of the signature, either. This means that it is not
permissible to create both a function and a subroutine with the same name and parameter list nor it is
permissible to create two functions that differ only in return type.
|