This seems silly

Blitz3D Forums/Blitz3D Programming/This seems silly

Picklesworth(Posted 2004) [#1]
Does anybody know why blitz can't use Gosub within a function?


Gabriel(Posted 2004) [#2]
It's bad programming practice. A function has to return a value, but if you were able to gosub back out of the function without returning a value and go absolutely anywere in the code, you would/could be storing up huge problems for yourself.

I can't really think of any reason you'd ever need to. Logically you would just call another function instead.


Jeroen(Posted 2004) [#3]
I hope Blitzmax gets rid of this goto/gosub "Nonsense".


WolRon(Posted 2004) [#4]
Not Goto, just Gosub.

Goto still IS useful from time to time.


Damien Sturdy(Posted 2004) [#5]
Gosub should go. Goto should stay. its useful for debugging :D


also..

LMFAO @ WolRon's Sig... i remember the post being made, but its taken me this long to realise what it actually said... hah. /me slow


jfk EO-11110(Posted 2004) [#6]
I agree, goto is the debuggers best friend. And to solve that problem, i'd suggest too: replace the Gosub command by a function call and make the part that is executed by the Gosub a function. Of course, make sure there are no Global vs Local troubles.


Picklesworth(Posted 2004) [#7]
I generally wouldn't use a subroutine, but I did at the point when I wrote it because I hadn't yet been hit by the revelation about how many ways Type instances can really be used.
Of course, my problem was easily fixed, but I just think that aside from the stupidity of subroutines, there should really be no reason why they can't be used within a function.


Regular K(Posted 2004) [#8]
I never have uses for Goto and I dont even know the difference between goto and gosub!

:o newbie me :P i guess im a newb if i dont know that


bradford6(Posted 2004) [#9]
how about this:


x = 5

goto skiplines

foo = 2
n = n -3
blah = blah/2

.skiplines








Regular K(Posted 2004) [#10]
I still dont understand how that will come in use.


IPete2(Posted 2004) [#11]
What is wrong with you people.

Gosub and Goto have their uses - at least for me.

Sometimes people just want to fix something which is not broke, I say leave it well alone. ;)

Sadly, Mark has already indicated that Gosubs and Goto's won't be in BlitzMax, although why really escapes me.

IPete2.


jhocking(Posted 2004) [#12]
For debugging. I prefer commenting lines I want to test running without because that gives me a clear visual indicator of what lines are not being executed, but for large blocks of code using goto saves time.

Oh, and ta-ta gosub. Good riddance to bad rubbish.


puki(Posted 2004) [#13]
Goto is handy for skipping parts of your program in testing stages - as shown by "bradford6".

'Gosbub' is cuter (being smaller) and shall be missed.


Perturbatio(Posted 2004) [#14]
Goto is handy for skipping parts of your program in testing stages - as shown by "bradford6".

x = 5

If FALSE Then
foo = 3*33
x=x+foo

EndIf

Print x



IPete2(Posted 2004) [#15]
I would be very interested in any explanation given which truly provides an adequate reason to get rid of both GOTO and GOSUB.

Any one?

IPete2.


Ross C(Posted 2004) [#16]
Goto should remain. Gosub too. :o) They ain't doing any harm.


Wayne(Posted 2004) [#17]
Gosub with a function?
No No No NO NO!


Curtastic(Posted 2004) [#18]
you should be able to gosub to a label that is inside the same function that you are in. but you cant

It wasnt until I coded my RTS that I found a reason for this. I only needed it once, but I needed it badly


Jeroen(Posted 2004) [#19]
Why not just improve debugging in Blitzmax?


jhocking(Posted 2004) [#20]
How about multiple line commenting? Like how C++ uses */ for comment blocks.


TomToad(Posted 2004) [#21]
Yeah, the /* */ command in C is great. Wish Blitz had it. :-) I know there's If False command, but with the /* */ you also get the block of code changing colors so you can see exactly what's being taken out.
As far as Goto and Gosub, I see no reason to take them out. You don't have to use them if you don't want to, but at least they'll be available for those one or two times they come in handy.


DrakeX(Posted 2004) [#22]
goto should still be there for those once-in-a-blue-moon occasions when regular loop structures just aren't enough. oh, and for 40-line tiny console programs. no use bothering with functions for something like that :)

honestly, though - i've seen plenty of bad code that had not one goto in it. will removing goto really improve anything? there will always be bad programmers.


BODYPRINT(Posted 2004) [#23]
I use Goto a fair bit for random number checking. I know i should use do/until's but Hey!! I like them.

e.g.

Check a random location.
If it's blocked, goto the random generation part again.

Is that bad?


Picklesworth(Posted 2004) [#24]
Harmless I think. It's just another way to write stuff. What's wrong with alternatives?


TeraBit(Posted 2004) [#25]
Goto still works in BlitzMax. No Gosub though :)


Rob Farley(Posted 2004) [#26]
Wolrons sig:
http://www.blitzbasic.com/Community/posts.php?topic=40166


John Pickford(Posted 2004) [#27]
I can't say it bothers me that these instructions exist but I haven't found a need for them. Gosub in particular is redundant when you have functions. Goto also seems pretty redundant as any conditional goto can be more elegantly be written with and IF\ENDIF. Unconditional Goto means your code should probably be rearranged.


Damien Sturdy(Posted 2004) [#28]
Unconditional Goto is good for debuging, surely?

Goto skipbiu  ;Detect if error is in this section of code

<routine>

.skipbit



John Pickford(Posted 2004) [#29]
I use:

If  0



endif


In fact, I wouldn't be surprised if Beaker does exactly the same thing.


I don't think I've ever used a .label in years of Blitz coding!


Beaker(Posted 2004) [#30]
Or:
If 0
;<routine>
EndIf



BODYPRINT(Posted 2004) [#31]
Beaker?? Thats exactly the same as John's post!!??


doctorskully(Posted 2004) [#32]
The reason why Goto and Gosub exist within Blitz, I think, is because the original B.A.S.I.C. (before Blitz BASIC) relied an awful lot on this. Besides, how would you move about in your program? You'd have to keep on typing the same code over again, or calling the same function, and it would generally be very confusing.

BUT...technically speaking, Goto's and Gosub's could easily be replaced by loops, "Include", or function calling. Instead of going back to .start, you could just make a Repeat...Until loop or a While...WEnd loop. But they're very handy for debugging, as said before, to completely skip over a piece of code that you suspect is runining your program.

Another thing--my theory about there not being a GoSub command allowed in a function is the fact that a value can be Returned, and you must use Return at the end of a GoSub, causing a fair amount of confusion for your poor computer which doesn't know which Return means what.

Of course, a quick fix for this is just to create another label at the end of code to return, such as:

Goto label1
.label2

.label1
;do something
Goto label2

This kinda is the same as the GoSub's purpose anyway.

But this is all just too much work, isn't it?


wizzlefish(Posted 2004) [#33]
Gosub just replaces functions. It should burn. To death.

Goto should live in peace on the magical island of Fincayra.


John Pickford(Posted 2004) [#34]
>But they're very handy for debugging, as said before, to >completely skip over a piece of code that you suspect is >runining your program.

Except an IF 0 is less typing and doesn't require you to think of a unique .label. So goto is totally redundant. ;-)


BlitzSupport(Posted 2004) [#35]

So goto is totally redundant



Not if you're being lazy and wrong, and using it to reset everything before the main game loop like I do (after game over or whatever).

Not every project has to be done 'correctly', eg. most of mine... :)

I think only poor ol' sswift uses Gosub! Makes no odds to me whether it's there or not.


Ross C(Posted 2004) [#36]
It's just another way to do something. As long as your code/app/games works, it doesn't really matter if goto was used :o)


Dreamora(Posted 2004) [#37]
gosubs are useless anyway.
why?
because the blit3d compiler checks sources top town -> gosubs need to be at the top to prevent variable redifinition problems and similar ...
and placing them a the bottom in a more structured way is normally exactly what you try to achieve with them in VB and similar ...


WolRon(Posted 2004) [#38]
Except an IF 0 is less typing and doesn't require you to think of a unique .label. So goto is totally redundant. ;-)
If you normally don't use Goto's then inserting one is in a way like the C++ /* */ comment block because it sticks out (especially if you don't indent it with the rest of your code).

If False or If 0 don't stand out as much.


also...

Goto's are FAST and don't require any use of the Stack, unlike Gosub and Functions which need to PUSH AND POP the stack every time they are used.

If you have a particular section of code that's time dependant then a Goto is a good choice.


Curtastic(Posted 2004) [#39]
I use if 0 and change it to if 1 a lot to toggle things when testing code
example:

if 0 ;hello hello look at me!!!!1111
you=createship("basic",10,20,4,5)
code()
else
you=createship("super",40,40,9,2)
othercode()
endif


John Pickford(Posted 2004) [#40]
>If False or If 0 don't stand out as much.

I think that's very weak to be honest. Totally subjective. I don't think either of them 'stand out' particularly. But theres nothing stopping you putting ;!!!!!!!!!!!!!!!!!!!LOOK AT ME!!!!!!!!!!! after either statement.

Speed is hardly a significant issue but I'd like to see an example where goto is a better choice than if/endif. Is there a speed difference?


big10p(Posted 2004) [#41]
I'd like to see an example where goto is a better choice than if/endif.


Erm, if/endif throws an error if you happen to wrap it around a Global definition. :P


DrakeX(Posted 2004) [#42]
the one thing that "if 0" can't do is comment out code that doesn't compile. like if you're testing out a new method of doing something, and you've changed parameters for a function, or the fields in a type, or something of the sort - it won't compile.

and you can't tell me that ;!!!!!!!!!!LOOK AT ME!!!!!! stands out just as much as a fifteen-line block of comments. ;)


John Pickford(Posted 2004) [#43]
And Goto will?


Dreamora(Posted 2004) [#44]
Wolron: Gosub has no stack, it runs the code on the actual stack. otherwise it would be totally useless.


John Pickford(Posted 2004) [#45]
>Wolron: Gosub has no stack, it runs the code on the >actual stack. otherwise it would be totally useless.

Eh? What does that mean? Surely Gosub translates directly to a JSR\CALL in assembler. Push the return address on the stack and Jump to the destination. RETURN simply popping the return address. When is code run on the stack?


big10p(Posted 2004) [#46]
Yes, John, you are right as far as I'm concerned. Dreamora, the stack is for temporary data storage - it doesn't contain code that can be executed. :/


Dreamora(Posted 2004) [#47]
John big10p:
If we are talking of stacks I thought of stacks like functions create them: An enclosed area of memory which holds all the local datas of the function and that is deallocated after the function exits again. This way local and global variables don't interfere with each other for example.

This part of memory that leads to stack overflows with over-recursived programs.


The mainprogram is the core stack
Every function adds a new memory block on this stack and removes it again on exit.

But gosubs don't create a memory block to the stack as they operate on the core stacks data ( which is the reason you can declare globals there, which is the reason you can change "normal" variables of the core program )


big10p(Posted 2004) [#48]
I got confused when I came to this line:
The mainprogram is the core stack

:/

Anyway, when you use a Gosub, the return address is stored on the stack so that the program knows where to continue execution from after the Return.


BlitzSupport(Posted 2004) [#49]
I used Goto in that RockOut game -- this kinda thing:

#LABEL ' Reset game...

lives = 5
shields = 100

Repeat

    Cls

    ' BLAH BLAH BLAH...

    If energy < 0 Then Goto LABEL

    Flip

Until KeyHit (KEY_ESCAPE)



If XXX = 0 isn't gonna do a damn thing here! Yes, it 'should' be done in other ways, but it's a piddly little game and this works nicely... and that's what matters.

Having the option to be a lazy programmer is a key part of Blitz as far as I'm concerned!


John Pickford(Posted 2004) [#50]
No way could I bring myself to do that James.


BlitzSupport(Posted 2004) [#51]
Ah, but the trick is... you don't have to!

I wouldn't do that for a 'proper' project, because I already know it can get clumsy, but for trivial stuff I like to take the easy way out.


DrakeX(Posted 2004) [#52]
"And Goto will?"

i believe we were talking about comments at the time.. a goto and an if 0 will get you the same results.. but a multiline comment block just makes the code go away. which is so much nicer and easier to see.


Strider Centaur(Posted 2004) [#53]
Core stack? You mean the Heap? The allocatable portion of memory used by applications?

As for Goto and Gosub, I think it matters not if they stay or go. I never use them, and I have never had to deal with other people using them. I can see some people still relying on Goto, I for one find that a bit odd with If, 3 or so types of loops and functions at you disposal, but to each their own.

I find the lack of strict variable definitions far more of a problem, especialy in debugging, than either of these 2 commands.

Also Comments are king, if you don't use them then your just not writing anything worth while, IMHO. LOL :)


WolRon(Posted 2004) [#54]
If we are talking of stacks I thought of stacks like functions create them: An enclosed area of memory which holds all the local datas of the function and that is deallocated after the function exits again
The stack doesn't contain any persistent variables really. It's use is for subroutines (gosub, function, etc.). Return addresses and passed variables are the only types of data (I believe) that get pushed or popped from the stack.
As Strider said, I think you were referring to heaps.

All in all, something that you usually wouldn't have to worry about anyways.