The RETURN of GOSUB

General discussion for topics related to the FreeBASIC project or its community.
DOS386
Posts: 798
Joined: Jul 02, 2005 20:55

SUB stuff

Post by DOS386 »

coderJeff wrote:
generates a runtime error "RETURN without GOSUB" if RETURN was used but no GOSUB call was made


A special stack for GOSUB'bing ? No direct translation into CALL and RET ?

v1ctor wrote
The machine stack is used to implement GOSUB RETURN, QB probably uses an user stack, what i won't add, sorry.
:-D
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

v1ctor wrote that he won't add it .. I didn't. :P

FYI, technical info:

In the ASM emitter, a hidden counter variable is created that keeps track of the gosub-call depth. But otherwise, CALL/RET instructions are used with the machine stack. The overhead (besides CALL/RET) is INC [counter] on each GOSUB, and IF( [counter] ) THEN DEC [counter] ELSE throw-error on each RETURN.

In the C emitter, a hidden pointer is created that points to a gosub-stack pseudo object. The "gosub" object saves state for each GOSUB with setjmp() and RETURNs from a GOSUB with longjmp(). So yes, this implementation uses an extra user-stack.

And of course, if GOSUB/RETURN is not used in a procedure, then no extra hidden variable/code is emitted.
Skyler
Posts: 242
Joined: Sep 26, 2006 16:30

Post by Skyler »

1000101 wrote: Erm, GOSUB creates just as much "spaghetti" as CALL. It just predates actual functions in BASIC is all. There is nothing inherently more or less "spaghetti" about it. I can make serious spaghetti with CALL, trust me.
I never said it was more spaghetti-esque than any other statement. I just said the gosub looked like a plate of spaghetti. But what happens if you CALL the GOSUB? *evil grin no. 2*
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Post by roook_ph »

I still am not convinced gosub should be removed
These codes bring nostalgia
Dim a as integer
Gosub Getfile
Gosub Process
Gosub Find
Gosub Sort
End

Getfile:
*** codes here**
Return
Process:
***codes here
return
Sort:
***codes here
return
I dont understand what spaghetti meant when using gosubs.If your teaching basic to newbies, its much easier because variables are not passed or localized.
Skyler
Posts: 242
Joined: Sep 26, 2006 16:30

Post by Skyler »

Spaghetti would be something like this:

Gosub noodle1


noodle1:
***random code here***
gosub noodle2
return

noodle2:
gosub noodle3
***random code here***
return

noodle4:
***random code here***
gosub noodle2
return

noodle3:
***random code here***
print "Sauce!"
return

Only probably a lot more complicated.
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Post by roook_ph »

Is there a unanimous agreement here to convert freebasic to gcc?
If thats the case then gosub is a lost cause. But That last snippet is crap. He probably wrote it for some basic with less than 100 built in functions . No .bi's and no .inc's . But still the logic seems intentional-to object adding criticized basic functions of the past .
robertsconley
Posts: 13
Joined: Dec 15, 2005 20:57

Post by robertsconley »

sir_mud wrote:GOTO hasn't and never will be removed from the default dialect, it has its uses. GOSUB however is generally outdated and only necessary for greater QB compatibility.
Gosub is used in various BASICs for the same reason as Nested SubRoutines. The situation that you need nested subroutines comes up often in math heavy routines.

A Nested subrountine looks like this

Code: Select all

Sub MySub (Arg1 as String, Arg2 as String)
  Dim I as Integer
  Dim TempI as Integer
  Dim Factor as Double
  Dim Result as Double
  Factor = 10
  Result = 0
  For I = 1 to 123
       TempI = I\ Factor
       Result = Result + NestedSub(TempD)
       
  Next I
  Function NestedSub(Arg1 as Integer) as Double
      Dim I as Integer
      Dim TempD as Double
      For I =1 to Arg1
            TempD = TempD + I/Factor
      Next I
      Return TempD
  End Function
End Sub
Note that any variable declared in MySub is shared between MySub and NestedSub. If a variable is declared in NestedSub that is the same name as in MySub then it becomes local to NestedSub.

Again I found this useful in organizing large and complex series of math heavy subrountines. For example in calculating the unfolded 2d layout of a 3d ductwork fitting.

With nested routines supported the need for GOSUB..RETURN goes away as there is now a construct that works the exact same way.
Hexadecimal Dude!
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:

Post by Hexadecimal Dude! »

Could you use a TYPE to achieve the same thing in a nicer way? eg:

Code: Select all

type foo
    Dim TempI As Integer
    Dim Factor As Double
    Dim Result As Double

    declare sub MySub(Arg1 As String, Arg2 As String)
    declare function NestedSub(Arg1 As Integer) As Double
end type

Sub foo.MySub (Arg1 As String, Arg2 As String)
  Dim I As Integer
  Factor = 10
  Result = 0
  For I = 1 To 123
       TempI = I\ Factor
       Result = Result + NestedSub(TempD)
       
  Next I
End Sub

Function foo.NestedSub(Arg1 As Integer) As Double
      Dim I As Integer
      Dim TempD As Double
      For I =1 To Arg1
            TempD = TempD + I/Factor
      Next I
      Return TempD
End Function
(Please note that I haven't error checked that code or tried compiling it)

Or does this not do quite what you want?
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Post by roook_ph »

I apologize i a have to criticize nested ifs. I find nested nested subroutine harder to read than the spaghetti code earlier. Especially if your in in the middle of the fourth ifs. By putting gosub in the bottom of your code you just edit the subroutine and you wont get lost in the web of ifs and select.
roook_ph
Posts: 402
Joined: Apr 01, 2006 20:50
Location: philippines
Contact:

Post by roook_ph »

types are just variables. You can make any variable global by using share command. Gosubs are line labels just like calling subs end sub or function end function. But instead , in gosub variables are not localized meaning you can dim variable inside a gosub and the variable name is still avaible after the ending statement which is return. Nested commands is an ideal replacement for gosub. But gosub in my opinion is better .
for example.
if a= b(0)
'sort routine here'
end if
you could type
if a=b(0)
gosub sort
end if
sort:
"sort routine here"
return
Since nested routine tends to have a lot of elseif inside theres a lot of tabbing . And finding the next elseif is harder the more complicated the code gets. There is no need for tabbing if you just gosub the major if and no need to remark each time you make in this instance sort routine.
Last edited by roook_ph on May 21, 2008 5:29, edited 2 times in total.
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Post by yetifoot »

I love the smell of gosub in the morning.
sbsalx
Posts: 1
Joined: Nov 11, 2016 15:30

Re: The RETURN of GOSUB

Post by sbsalx »

Gosub in Freebasic Language "FB"

Code: Select all

#DEFINE _GoSub Asm Call
#DEFINE _Label Asm
#DEFINE _Return Asm Ret
Dim C As Integer = 5
For I As Integer = 0 To 5
_GoSub CounterDown
Next I
Sleep
End
_Label CounterDown:
Print C
Sleep 1000
C -= 1
_Return
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: The RETURN of GOSUB

Post by coderJeff »

This suggestion(?) sbsalx has posted ignores some fundamentals of the fb dialect. This use of gosub/return does not work with fb dialect in all cases; primarily with scopes and variable allocation on the stack.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: The RETURN of GOSUB

Post by fxm »

A bit simplistic compared to what had already been suggested in 2006!!!
GOSUB in SUB
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: The RETURN of GOSUB

Post by Munair »

Honestly I do not see any need for GOSUB in procedural languages much less OO capable languages. It is a relic from the GW-BASIC era where every statement had a line number and DEF FN came closest to modern day functions.

GOTO on the other hand is powerful and can be handy at times inside (small) code blocks, provided you know what you're doing. While the feature has been the cause of much debate and BASIC's bad reputation, it is just as legitimate as ASM jumps.
Post Reply