GUI – Functions and Subroutines

The executable supports the notion of a function and of a subroutine.

To be honest, I have given up using subroutines.  From this executable’s point of view the only difference is that a subroutine does not return a value.


Subroutine

An example of a subroutine to display two text strings together via the message command is given below:

sub shownames ( firstName, surname )
Message = firstName & " " & surname
end sub

and could be invoked by a command such as call shownames ( "Abcd" , "Efgh" )

Note: is not necessary to specify the type of a parameter, it is optional.  Providing a type has the advantage of forcing a type consistency check when the subroutine is invoked.

If we redefine the subroutine with typed parameters:

sub shownames ( firstName as string , surname as string )
Message = firstName & " " & surname
end sub

and then invoke it by a command such as call shownames ( "Abcd" , 123 ) then an error is triggered.

The keyword call is necessary for a subroutine!


Function

Example of a function to return the length of two text strings:

function shownames ( firstName , surname )
shownames = length(firstName & surname)
end function

This function could be activated by a command line such as:

message = shownames ( "Abcd" , "Efgh" )

Note: just as for subroutines, is not necessary to specify the type of a parameter, it is optional.  Giving a type has the advantage of forcing a type consistency check when the function is invoked.

Further it is not necessary to follow the parameter list with the type of the returned value. But if it is given then it will trigger a type consistency check each time a return value is prepared.

The following will fail at runtime because the line  shownames = length(firstName & surname) provides a numerical value as the return value.

function shownames ( firstName , surname ) as string
shownames = length(firstName & surname)
end function

The following will  work:

function shownames ( firstName , surname ) as int
shownames = length(firstName & surname)
end function


Function & Subroutine naming

The rules for subroutine and function names are: that the name must meet the requirements of a keyword:

  • case-insensitive
  • must not be numerical
  • must not contain any spaces
  • must not contain any of the following characters + – * ‘/ “

A name can be re-used, in which case only the most recent definition is used.


Parameters
  • The parameter  name must meet the requirements of a keyword.
  • Parameters do not need to be given a type! Valid types are those applicable to a scalar variable created by a Dim command.
  • Any, all, or none of the parameters can be defined as optional.
  • Optional parameters must be given a default value.

An example of a functional definition where a  parameter is optional is:

function shownames ( firstname , optional middlename = "" , surname)
shownames = firstname & "-" & middlename & "-" & surname
end function

This function could be activated by a command line such as:

message = shownames ( "Abcd" , "Wxyz" , "Efgh" )
message = shownames ( "Abcd" , , "Efgh" )

Note: in one particular case I wanted to call a function that had lots of parameters, of which most were optional, and I only needed to provide a few values. I rapidly lost view of which parameter was which.

So I decide to implement a special no-value command NOP which can optionally be provided with a description string.

The last example could have been written as any of the following:

message = shownames ( "Abcd" , , "Efgh" )
message = shownames ( "Abcd" , NOP , "Efgh" )
message = shownames ( "Abcd" , NOP("middle name") , "Efgh" )