Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [BUG] Gosub throws a error on exit

Author
Message
Green7
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: Switzerland
Posted: 16th Sep 2017 12:41
this code throws "Encountered a RETURN statement when not in a GOSUB in at line 0" at me when i exit by "esc" key. guess gosubs are not that fancy...?

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 16th Sep 2017 13:10 Edited at: 16th Sep 2017 13:11
not a bug

you forgot the end after the loop, so it run in the test: without a gosub call , thats why return is confused

loop
end
AGK (Steam) V2017.08.16 : Windows 10 Pro 64 Bit : AMD (17.7.2) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 18th Sep 2017 09:13
GoSubs are evil. (Personal opinion.)
Click here for VR Legend of Zelda stuff
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 18th Sep 2017 14:16

so use more of them ?

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 18th Sep 2017 17:19 Edited at: 18th Sep 2017 17:20
Your code exits the do-loop when the user hits esc, so the execution continues after the LOOP... eventually hitting the spurious RETURN after displaying the frame rate one last time. If you want to get all retro with GOSUB, then why not throw in a GOTO as well! Replace "then exit" with "then goto finish", and add another label at the bottom of your code:

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th Sep 2017 17:31
This is a code diagram of what CJB said:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 18th Sep 2017 18:07
Quote: "
SetWindowSize(1024, 768, 0)
repeat
Print(ScreenFPS())
Sync()
until GetRawKeyPressed(27)
"


before code diagram
AGK (Steam) V2017.08.16 : Windows 10 Pro 64 Bit : AMD (17.7.2) Radeon R7 265 : Mac mini OS Sierra (10.12.2)

Attachments

Login to view attachments
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 19th Sep 2017 03:15
I haven't used gotos or gosubs since my learning darkbasic days, but I don't think gosubs are really that bad. IMO theres not much difference visually between CjBs code and this.


Goto's on the other hand, should be avoided like the plague, as that does lead to creating unmaintainable mess.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 19th Sep 2017 07:35
OT
Quote: "lead to creating unmaintainable mess."

agree, its better to encapsulate a task, you see what go in and come out.

pro function
encapsulate
can used recursive
versus
speed / overhead in design

pro gosub
speed
versus
encapsulate
AGK (Steam) V2017.08.16 : Windows 10 Pro 64 Bit : AMD (17.7.2) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 19th Sep 2017 08:16 Edited at: 19th Sep 2017 08:17
Oh no!!


The Goto/Gosub debate has reared it's ugly head again!

Personally, I don't use them but to play Devil's advocate, here's a time when using Goto actually makes sense:

Although, to be fair, I believe that AppGameKit handles all of that internally anyway.

The original question has been answered - the code drops out of the loop and then hits a Return without jumping there from a Gosub - hence the error.
Can we now all agree that Goto and Gosub have their uses but most seasoned coders tend to avoid them?
Green7
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: Switzerland
Posted: 19th Sep 2017 08:27



thanks for all the suggestions... i have now chosen to use a function instead.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Sep 2017 08:42 Edited at: 19th Sep 2017 08:43
The only language I have needed to use goto in, is VBA (Visual Basic for Applications). This language is missing exit commands from loops.

This code:



can be written as:



I don't know the full technical details, but exit will do a tidier job than goto, which will leave rubbish on the stack.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Supertino
6
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 19th Sep 2017 09:13
GoTo's have a place I think , I've used them to quickly escape a 'multi-level' for\next loop within a do\loop just slap the goto label after the loop and I can get out of the whole thing with a single call. That's the only scenario use them in though for readability Functions are superior.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 19th Sep 2017 10:05
Meh.

Gosub and Goto = Old
Functions = New

Both can = spaghetti if in the wrong hands.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 19th Sep 2017 10:09
Quote: "GoTo's have a place I think ,"

No.
Click here for VR Legend of Zelda stuff
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 19th Sep 2017 10:21
Quote: "No."
Lol.... you can exit a for loop in VBA using "exit for".
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Sep 2017 12:57
Quote: "you can exit a for loop in VBA using "exit for""


You can't continue a For loop either (i.e jump to the next iteration).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kevin Cross
20
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 26th Sep 2017 13:40 Edited at: 26th Sep 2017 13:44
@CJB you can't exit all loops when nested using exit on it's own. It just exits the loop it's added to i.e. the below code will still loop through x 2 to 5.



This will print

1,1
2,1
3,1
4,1
5,1

There might be times when you want to jump out of both loops which is when a goto works



This will print

1,1

One example would be searching a grid for an item/object/value and exiting when you've found the x and y position in that grid without checking the rest of the grid.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 26th Sep 2017 13:55
@Kevin Cross: When I need to exit nested loops I just manually finish the for loop by setting the iterated variable to its max value.

Kevin Cross
20
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 26th Sep 2017 14:00
@ShaunRW thanks for the tip.
tmu
7
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 26th Sep 2017 17:33
Goto the corner. You have been very naughty.
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 26th Sep 2017 22:35
By @BatVink:
Quote: "The only language I have needed to use goto in, is VBA (Visual Basic for Applications). This language is missing exit commands from loops.

This code:


can be written as:



I don't know the full technical details, but exit will do a tidier job than goto, which will leave rubbish on the stack."


Whilst certainly an improvement, arbitrary exit points is nearly as bad as arbitrary entry points (which is the real reason GOTO is pure distilled evil, as it combines both traits).

What I'd do is simpler and safer:



Allthough to be honest, in a real world application, I'd extract all the input logic to some function, and set a flag that gets checked the status of at the end.

And since we've established a long time ago that GOTO is evil, it's little brother the GOSUB is at least deviously mischievous

Functions rules - at least in AppGameKit which doesn't have a trace of OO in tier 1. Not that that is a bad thing as such, but there is a tool in my box of tricks missing...

Login to post a reply

Server time is: 2024-04-18 13:36:43
Your offset time is: 2024-04-18 13:36:43