|
Psub | |||||
Psub <.PsubName.>[(ParamList)] ... [Return] ... EndPsub | |||||
Parameters: NONE | |||||
Returns: NONE | |||||
Psubs (protected sub-routines) are really a variation of the classic Gosub / Return pairings also known as a sub routines that can be found in traditional BASIC's, with one major exception, they're required to be defined within Psub / EndPsub blocks. So Psubs look more like functions than sub routines do. (See Functions for more in depth info about functions) To define a psub, we first need to give the sub routine a Name and optionally a list of parameters that would be passed to the sub routine when it's called. The following shows a sample Psub template of a routine called "Test" which has no parameters.
The above code just declares the entry and exit points of the PSUB named TEST. Apart from that, if you executed this in PlayBASIC, the program wouldn't seem to do anything. That's because, we need to call the psub in order for the code inside it to be executed. To call any psub, we use the subs name, followed by it's list of input parameters enclosed within brackets. Now since this example doesn't have any input parameters, to call this sub would simply be Test(). So when PlayBASIC executes this statement in your program, execution control is changed from the current place to that of the sub routine. When the PlayBASIC reaches the end of the sub routine, control reverts back to after the calling statement. Here's a version of previous example, but this time we've fleshed it out some more with a call to the sub routine and we've added some code to display (print) a message when the call is executed.
PSUBS handle input and return parameters the same way that functions do. So rather than go over that again, we recommend you read the page on functions, which covers this in greater detail. The following example, shows a psub with a pair of integer input parmeters and a single integer return, with calling example.
FACTS: * PSUBS are similar to Functions. The primary difference is that all variables inside the psub are static. That is to say, between each call all variable retain their existing information. You can declare variables as Static and do the same thing in a function. * PSUBS generally execute faster than Functions. So if you don't need local variables (and many functions don't) then you get an easy bit of performance gain just by using Psubs. * For an overview of function and psub parameter passing, please see the Functions&PSub tutorial. * Protected Subroutine declaration keywords (Psub & EndPsub) can not be indented. * Variables & Arrays defined within a Psub are ALL static by default. * You can return multiple values from a Psub. Mini Tutorial:
This example would output.
|
Related Info: | CallFunction | EndPsub | Function | Functions&Psub | Gosub | Goto | Return : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |