Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:20 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : What’s wrong with my GOSUB?

Author Message
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 04:37pm 30 Sep 2015
Copy link to clipboard 
Print this post

Hi all,

Here is a simple test of ON GOSUB

' Test of GO SUB

Do
kb$ = Inkey$
If kb$ <> "" Then Print ">";kb$;"<== Pressed!"
On Val(kb$) GoSub Menu1,Menu2,Menu3,Menu4,Menu5,Menu6
Loop

Sub Menu1
Print "Menu 1"
End Sub

Sub Menu2
Print "Menu 2"
End Sub

Sub Menu3
Print "Menu 3"
End Sub

Sub Menu4
Print "Menu 4"
End Sub

Sub Menu5
Print "Menu 5"
End Sub

Sub Menu6
Print "Menu 6"
End Sub


Here is two attempted runs

> run
>1<== Pressed!
[6] On Val(kb$) GoSub Menu1,Menu2,Menu3,Menu4,Menu5,Menu6
Error: Cannot find label
> run
>2<== Pressed!
[6] On Val(kb$) GoSub Menu1,Menu2,Menu3,Menu4,Menu5,Menu6
Error: Cannot find label
> ? mm.ver
4.05
>

I am using a CGCOLORMAX 2 running the above version.

What is it that I do not understand?

Rob

Rob White
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:03pm 30 Sep 2015
Copy link to clipboard 
Print this post


' Test of GO SUB

DO
kb$ = INKEY$
IF kb$ <> "" THEN PRINT ">";kb$;"<== Pressed!"
ON VAL(kb$) GOSUB Menu1,Menu2,Menu3,Menu4,Menu5,Menu6
LOOP

Menu1:
PRINT "Menu 1"
RETURN

Menu2:
PRINT "Menu 2"
RETURN

Menu3:
PRINT "Menu 3"
RETURN

Menu4:
PRINT "Menu 4"
RETURN

Menu5:
PRINT "Menu 5"
RETURN

Menu6:
PRINT "Menu 6"
RETURN


I would use SELECT CASE rather than ON GOSUB then you can use SUB....END SUB

Edit: Select case is not available for MMBasic V4.5

JimEdited by TassyJim 2015-10-02
VK7JH
MMedit
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 05:21pm 30 Sep 2015
Copy link to clipboard 
Print this post

Thanks Jim,

Yes I would use Select Case too if I could. Don't like labels much!

Thanks

Rob

Rob White
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 07:34pm 30 Sep 2015
Copy link to clipboard 
Print this post

You could try resolving Val(kb$) before the ON command.

[code]
Do
kb$ = Inkey$
If kb$ <> "" Then Print ">";kb$;"<== Pressed!"
kb = val(kb$)
On kb GoSub Menu1,Menu2,Menu3,Menu4,Menu5,Menu6
Loop

[/code]
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 07:52pm 30 Sep 2015
Copy link to clipboard 
Print this post

Another possibility. I like to change strings into numbers as quickly as possible because strings are pretty slow in processing. (Not so important here).
[code]
DO
key = ASC(INKEY$) - 48 ' "1" = ascii code 49
IF key = 1 THEN
Menu1
ELSE IF key = 2 THEN
Menu2
ELSE IF key = 3 THEN
Menu3
ELSE IF key = 4 THEN
Menu4
ELSE IF key = 5 THEN
Menu5
ELSE IF key = 6 THEN
Menu6
ELSE
'Illegal key pressed
'Do something with it or ignore
ENDIF
LOOP
[/code]

Microblocks. Build with logic.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2944
Posted: 08:50pm 30 Sep 2015
Copy link to clipboard 
Print this post

@Bizzie

Answering your original question - you have no RETURN (hence TassyJim's post sorting this out).

I am sure you've managed to sort it all out by now but for completeness here is a listing using SELECT CASE that you referred to:

' Test of GO SUB

Do
kb$ = Inkey$
If kb$ <> "" Then Print ">";kb$;"<== Pressed!"
kp = ASC(kb$) - 48 ' "1" = ascii code 49
select case kp
case 1
Menu1
case 2
Menu2
case 3
Menu3
case 4
Menu4
case 5
Menu5
case 6
Menu6
case else
'Illegal key pressed
'Do something with it or ignore
end select
Loop

Sub Menu1
Print "Menu 1"
End Sub

Sub Menu2
Print "Menu 2"
End Sub

Sub Menu3
Print "Menu 3"
End Sub

Sub Menu4
Print "Menu 4"
End Sub

Sub Menu5
Print "Menu 5"
End Sub

Sub Menu6
Print "Menu 6"
End Sub


WW

EDIT: Not sure if v4.05 has the SELECT CASE function Edited by WhiteWizzard 2015-10-02
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:11pm 30 Sep 2015
Copy link to clipboard 
Print this post

  WhiteWizzard said  

EDIT: Not sure if v4.05 has the SELECT CASE function

No.

If ...Then
..
ELSEIF ...
..
ENDIF

is the alternative.

Jim
VK7JH
MMedit
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 09:57pm 30 Sep 2015
Copy link to clipboard 
Print this post

Thanks to all who have responded.

All sorted now.

Regards

Rob

Rob White
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025