|
Gosub | |||||
Gosub <.label.> | |||||
Parameters: NONE | |||||
Returns: NONE | |||||
Gosub is an unconditional change of control with return mechanism. So Gosub allows us to jump from the current position in our program to another section, performing some task, then Return back to point after the gosub call and continue on. These tasks are often called "Sub Routines" and allow us to take a repeatable fragment of code and reuse then. To create a Sub Routine we need two things, we need a entry point (a label to jump to) and our closing return (exit point) statement. (1) The Label is the start of the section of code we wish to call / reuse (The entry point) (2) A RETURN statement at the end of the gosub. (The exit point) Gosub / Return Sample: Here's some sample code that shows how a simple sub routine can be called using Gosub with Return continue on from the call point.
Gosub Related Programming Errors It's important to note that while using sub-routines can allow us a very free form style of programming, compared to Functions() and Psubs(). This freedom comes at a cost. It's not possible for PlayBASIC to detect errors in your logic when building, calling or returning from subroutines during the compilation of your program . So If you've made a boo-boo then it'll probably only raise it's ugly head during testing. The two most common issues with Gosub's are Stack Under and Over Flows. To understand them, we need to dig a bit deeper into how Gosub actually works behind the scenes. When a Gosub command is executed, PlayBASIC pushes the current Address ( the position in memory of the gosub call ) onto the STACK (an area in memory where temporary information is stored in a big heap). After the push, control is then changed to the sub routine. In other words, It first remembers where it should return to, then jumps to the subroutine code. When PlayBASIC reaches a RETURN command, it does the opposite. So this time, it Pops the top value off the stack. If you think about, This value should be location (address) of the original Gosub call. Now once it has the address, it restores execution to the next command after the original gosub call. This is where the problems lie. If you call a subroutine and never return. Then the stack is slowly or rapidly filling up with data (return addresses) which are never being removed. Sooner or later the stack will overflow (fill up). Producing the "Stack Overflow" runtime error. Alternatively, if you attempt to Return from a sub-routine without ever gosub-ing into it in the first place, then you'll get a stack underflow error. Which means the stack was empty when Return tried to remove the top item from it. So when using gosubs, we have to be careful that our program is jumping and returning correctly. I'd recommend trying to structure your sub-routines so that they a top-down flow about them. Preferable with one entry point and one exit point. The more entry points and exit points you have in your sub-routines, the harder they can be to keep the program flow logical. Often many of above stack problem occurs due to program logic being faulty. Which is something to be aware of. Sub-Routine Labels: While Labels can be placed virtually anywhere through your program, there are some limitations you should be aware of! Which are, 1) Labels can not be indented. 2) Goto/Gosub can only jump to labels within the same scope. So you can't call a label inside a function/psub if you've outside of it and vice versa. These have been put in place to help prevent some unsafe programming practices. FACTS: * Gosub expects the labels you give it, to be within the current scope. * Opposed to Psubs and Functions calls you can't pass parameters * When using Gosub, make sure the Label you are calling is NOT indented. * Gosub is fall back command from old school editions of the BASIC langauge. Today, it's recommended that you use Function's or Psub's to create reusable sections of code. See Functions&Psub for a tutorial. * Labels are declared with a name followed by a : character (See ProgramLayout) Example #1
This program would output.
Example #2 The differences of Gosub and Goto
This example would output.
|
Related Info: | Function | Functions&Psub | Goto | ProgramLayout | Psub | Return : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |