Goto Gosub differences?

Community Forums/General Help/Goto Gosub differences?

Captain Wicker (crazy hillbilly)(Posted 2012) [#1]
what is the difference between goto and gosub?


Yasha(Posted 2012) [#2]
Goto is a blind jump.

Gosub leaves a return address on the call stack, so that when you're done, you can return (in more or less the same way as a function does) and carry on the code from after the original jump left off.

Much like recursive function calls, millions of unreturned Gosubs will eventually break the program (although actually doing this would be an achievement in itself...). Presumably because it uses the call stack, and the same keyword to return, it is not possible to use Gosub from within a function body (Goto can be used within a function so long as you jump only to somewhere else in the function - label names are locally scoped).

BlitzMax does not support Gosub at all, whereas in non-Strict mode it does still offer Goto. Gosub is not commonly supported in non-BASIC languages (e.g. the C-language family has no equivalent), mainly because it's what BASIC used to use before it got proper functions; most other languages evolved with functions from the start.

There is no legitimate excuse for using Gosub in new Blitz code. It exists pretty much solely to provide an easy compatibility layer with older (by now, really old) BASIC programs, written in a time when either functions weren't available, or users hadn't learned them yet. (Do not take this to mean you should master Gosub before functions: you should not use Gosub at all.)

In contrast, I can think of precisely one place where Goto would still be commonly considered acceptable in Blitz Classic (to replace the absent Continue command), which is in turn no longer necessary in BlitzMax (on account of it having the aforementioned Continue command).

Last edited 2012


(tu) ENAY(Posted 2012) [#3]
Christ, not sure why you would be asking this sort of question unless we're back in the 80s. :)

In very simple terms.

A Gosub is a bungee jump.
A Goto is a bungee jump without a rope.

:)


Captain Wicker (crazy hillbilly)(Posted 2012) [#4]
ah, that clears things up. thanks! :D


Adam Novagen(Posted 2012) [#5]
... I can think of precisely one place where Goto would still be commonly considered acceptable in Blitz Classic (to replace the absent Continue command)...
I am proud to say that this is in fact the one and only area where I still use Goto, and have been doing so for about a year and a half before I had even heard of Continue simply because it seemed to be the only natural solution. I may have taken the long way around to do it, but I find that nowadays, much of "good coding practice" is naturally starting to run in my blood. :D

A Gosub is a bungee jump.
A Goto is a bungee jump without a rope.
You deserve a cookie. :]


Hotshot2005(Posted 2012) [#6]
Avoid Goto or even Gosub.....used Functions instead :)


D4NM4N(Posted 2012) [#7]
-Actually a gosub is a bungie jump with an optional / possibly untied rope. ;)

But yes, avoid!

Gotos are really bad, if you need to use one (in this day and age) you are doing something VERY VERY wrong.

A gosub is basically a parameterless function, but still, same rules as above, except it is not forced to return, making it like a goto with an optional edit: non-enforced "return to call point"... which is also bad bad bad.

Last edited 2012


TaskMaster(Posted 2012) [#8]
They are identical in the fact that you should not be using them.


Yasha(Posted 2012) [#9]
optional "return to call point"


Returning from a Gosub is not optional any more than returning from a function is optional. If you use Gosub as a direct jump throughout your program and never return, that's more or less the same as using infinite recursion and the program will probably crash.

If nobody's seen this... It's probably a testament to how hard it is to write nontrivial programs using Gosub.

Either way the fact it's not obvious just gives another reason to avoid them.

Last edited 2012


D4NM4N(Posted 2012) [#10]
No it is not, but it can run on. (at least in archaic basic interpreter land, meaning it never gets called off the stack leading to as you say.. a probable crash). Although debugging the bugger might lead you to jump without a rope off the top of your building :D). Although iirc it will only crash if large/deep enough to stack overflow

You are right though, optional is not the right word..... um... i have changed to "not strictly enforced"

I don't think i have used a goto or gosub in 20 years :/

Last edited 2012


-=Darkheart=-(Posted 2012) [#11]
I think you are all being a bit harsh; remember one of the benefits of a Blitz is that it is very newbie / retro friendly and new, inexpereinced programmers may well use gosub / goto in their early stages because they offer simple, easy to understand methods of achivieing what they want to do.

I accept it is bad style but I think it is wrong to suggest they should never be used at all. It all depends on the context. If someone is programming early on they will probably not use advanced methods to do so, particulalry if they are learning for themselves and not just copying someone else.

To summarise: Using these commands is a bit like sucking your thumb, a natural early step and relatively harmless as long as you grow out of it in the long run. As someone who had to transition between the two it took a while until I was ready to completely leave behind these commands and the programs I wrote with them still work just as well as the ones I write now which don't.

On a practical note, if you are going to use GOTO or GOSUB you can make your life easier by putting the labels you will need to use these commands in a specific place, ideally at the end of your program or you can end up with some strange behaviour. Also be aware that labels in Blitz are caseless so .FLIPPER = .flipper

Darkheart


D4NM4N(Posted 2012) [#12]
As you say it is bad style. So why would anyone reccommend using it, especially to someone who is new to programming. :/

I cannot see ANY benefit of using them at all when you have comments, functions, a variety of loops, switches and else statements.
There is no palletable reason for them to even exist in the command set.


GfK(Posted 2012) [#13]
Every time you use a GoTo, I murder a puppy.

Use a Gosub, I murder a kitten.

That's the difference.


Yasha(Posted 2012) [#14]
If someone is programming early on they will probably not use advanced methods to do so


If someone is programming early on, they will make mistakes, yes.

Goto and Gosub don't provide any kind of path into learning structured programming. They aren't "on the road to" good style. They are simply bad style and it ends there. So if a new programmer uses them, they aren't really learning anything that will help them improve. The reason it took you a while to move on is probably because you'd already learned the behaviour.

Besides, if you can understand Gosub, you can understand functions. Functions are not complicated - in fact their behaviour is a lot simpler than Gosub - although they are one of those things (alongside types) that the Blitz manual has for some reason decided to scare new users away from, by calling them complicated and then providing an utterly baffling explanation.


Captain Wicker (crazy hillbilly)(Posted 2012) [#15]
Every time you use a GoTo, I murder a puppy.

poor puppy. :P


Jason W.(Posted 2012) [#16]
haha..this topic sparked my interest. I thought I was a kid again using my C64 :P

Jason


dawlane(Posted 2012) [#17]
Naff eighties program to follow....
5 CLS
10 PRINT "WHY"
15 GOSUB 55 
20 PRINT "GOTO & GOSUB" : GOSUB 65 : GOSUB 75
25 PRINT "ARE" : GOSUB 60 : GOSUB 50 
30 PRINT "TO"
35 GOTO 80
40 GOSUB 70 
45 GOTO 10
50 PRINT "WAYS"
55 PRINT "USE" : RETURN
60 PRINT "BETTER" : RETURN
65 PRINT "WHEN": RETURN
70 PRINT "<8-O" : PRINT : RETURN
75 PRINT "THERE" : RETURN
80 PRINT "DO IT?" : GOTO 40



D4NM4N(Posted 2012) [#18]
Arrrrgh!


Hotshot2005(Posted 2012) [#19]
GOSUB or Goto? I wouldnt used it today in any Blitzbasic but I do know some retro Coder used 8 bit computer or emulator to program today games!

Last edited 2012


Yue(Posted 2012) [#20]
for a time use these commands as a rookie, of course I'm still a rookie, sources when I discovered how to exit a loop with "Exit" and no longer use.