Using GOSUB

Post

Posted
Rating:
#1 (In Topic #257)
Regular
Doctor Watson is in the usergroup ‘Regular’
I would lke to include some 'subroutines' using GOSUB but can't get it to work. I searched all of Gambas documentation (Wikis, …) but nothing I find there works.
So here is the most simple code that works in most other Basics, but doesn’t in Gambas:

' Gambas class file
Public Sub Form_Open()
  GoSub Display
End

Display:
'Execute some code and return
Return

When I try to run this, I get “Missing AS in Fmain.Class:10”
'10' being the line number of Display:

This will look peanuts to an experienced Gambas user …  :shock:

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
Cedron is in the usergroup ‘Regular’
I love GOSUBS.  They make BASIC better than any other language.  Java used to have them in the byte codes, but never implementtted and now deprecated.

Gosubs are local within a routine, just like with VB.

Code (gambas)

  1. '==================================='
  2. Sub This()
  3.  
  4.     blah
  5.     blah
  6.    
  7.     GoSub A
  8.    
  9.     blah
  10.    
  11.     Return
  12.    
  13. '-----------------------------------'
  14. A:
  15.  
  16.     more blah
  17.  
  18.     Return
  19.  
  20. '==================================='
  21.  

The separator lines aren't necessary, but I usually run them out to column 79.

The "Return" statement is used for both an "Exit Sub" and a gosub "Return", determined by context.  The scope of variable is determined by the sub, that's what is so nice about them.

Ced

.... and carry a big stick!
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
Doctor Watson is in the usergroup ‘Regular’
 Quod erat demonstrandum.
Let's see if I can put it to use.
Thanks Cedron

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
I find the trouble with GOSUB is that you can only use it in the subroutine your in. If you create another subroutine instead you can call it from any part of your program. Example: -

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3. For iLoop As Integer = 33 To 126
  4.   Print GetAscii(iLoop)
  5.  
  6.  
  7. Public Sub GetAscii(iValue As Integer) As String
  8. Dim sReturn, sCase As String
  9.  
  10. sReturn = Chr(iValue) & " is ASCII value " & Str(iValue)
  11. If IsLower(Chr(iValue)) Then sCase = " lower case"
  12. If IsUpper(Chr(iValue)) Then sCase = "n upper case"
  13. If IsNumber(Chr(iValue)) Then sReturn &= " and is a" & sCase & " number"
  14. If IsLetter(Chr(iValue)) Then sReturn &= " and is a" & sCase & " letter"
  15. If IsPunct(Chr(iValue)) Then sReturn &= " and is punctuation"
  16.  
  17. Return sReturn
  18.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Regular
Cedron is in the usergroup ‘Regular’
GoSubs allow you to completely avoid overburdened control structures.

I've seen them, you've seen them, "If" statements that span pages. "If" and "Do" and "Select"s and "For" structures nested way too many levels deep.

Suppose a monster like this starts growing in your code.  You add features, make exceptions, etc. etc., the code grows.  So, to tame it, you want to take a chunk of it out, put it aside under a label, and leave the label behind.

In most languages, other than BASIC and Assembly, you only have one choice in the kind of label to put it behind.  A full blown separate procedure, complete with calling arguments needing to be defined at the call, at the definition, maybe other overhead, etc. etc.  The threshold for this being rather high is the reason you see so many overburdened control structures.  

Of course this solution is available in BASIC, but BASIC also gives you the GoSub.  Simply cut and past the code to the bottom of your sub.  Place a "Return" line in front, a label for the Gosub, your pasted code, another "Return", then put "Gosub label" where the code was and you are done.  Well, you'll likely change the indentation and comment it more.  If it isn't the first GoSub, the first "Return" won't be necessary.  It is cheap at runtime too, (I presume this is true for Gambas as well).

So how do you decide which type of GoSub to use?  It depends mostly on two criteria:

1) How many local variables are in the code that you will need to define references for?

2) Is it useful as a generally available sub?

Those criteria aren't quite independent either, so most the time the decision is easy.

Here's an example from the GamePad test program I am working on.

Code (gambas)

  1. '=============================================================================
  2. Public Sub TimerTick(ArgDisplayLB As ListBox, ArgTagNamesCB As CheckBox[])
  3.        
  4.         Dim theOutcome As Integer
  5.        
  6.         Do  
  7.            theOutcome = myGamePad.GetFancyEvent()
  8.            If theOutcome < 0 Then Break
  9.            GoSub ShowInDisplayListBox
  10.            GoSub HandleGamePadEvent
  11.         Loop
  12.  
  13.         Return
  14.        
  15. '-----------------------------------------------------------------------------
  16. ShowInDisplayListBox:
  17.  
  18.         ArgDisplayLB.Add(myGamePad.EventToString())
  19.        
  20. '---- Maybe Reduce Event Display Size
  21.  
  22.         If ArgDisplayLB.count > 1000 Then
  23.            ArgDisplayLB.Remove(0, 100)
  24.         Endif
  25.  
  26. '---- Position Display At End
  27.  
  28.         ArgDisplayLB.Index = ArgDisplayLB.Count - 1
  29.  
  30.         Return
  31.        
  32. '-----------------------------------------------------------------------------
  33. HandleGamePadEvent:
  34.  
  35.         Dim n, v, k, e As Integer
  36.        
  37.         n = myGamePad.MyGPE.Number
  38.         v = myGamePad.MyGPE.Value
  39.         k = myGamePad.MyGPE.Kind
  40.  
  41.  

There are many more lines, and a few more gosubs that follow.  All too big to fit in the read loop.  Yes, in this case there weren't too many parameters to worry about, but keeping it local keeps it neater and the external interface cleaner.

It's really nice to have GoSubs as an option.

.... and carry a big stick!
Online now: No Back to the top
1 guest and 0 members have just viewed this.