Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:28 11 May 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 : MMBasic Subroutines

Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 01:21pm 30 Jun 2014
Copy link to clipboard 
Print this post

I don't know why I have a mental block on this but I hope someone can tell me the difference...

When would I use this...

IF A = 1 THEN GOSUB THISONE


SUB THISONE
P = A + 5
END SUB

or this

IF A = 1 THEN THATONE

THATONE:
P = A + 5
RETURN

When would I use END SUB and when would I use RETURN?
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9486
Posted: 01:26pm 30 Jun 2014
Copy link to clipboard 
Print this post

I used to be the biggest GOSUB fan, but have dropped all GOSUB's now for SUB's.

What I really like about a defined sub or function, is that you can pass variables to the sub. I guess you could do the same with a GOSUB, but define your variables first, then call the GOSUB, but the SUB or FUNCTION way is much neater - that's one main reason I like them.

It does tend to be a personal choice, to some extent, and you could reasonably assume that GOSUB/RETURN was the old BASIC way of doing subs, and SUB/END SUB etc is the modern BASIC way of doing things.

Both your examples would work.

EDIT: No, the first example would not work, as you have an END SUB for a GOSUB, which is confusing the two. You need END SUB on the first example. Actually, I would need to check that one, as it MIGHT work(as a SUB and GOSUB are both subroutines, and RETURN and END SUB both do the same kind of thing), but I would expect not.

EDIT: I am confusing MYSELF now!!!! I will wait for someone else to comment!Edited by Grogster 2014-07-01
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6220
Posted: 01:59pm 30 Jun 2014
Copy link to clipboard 
Print this post

For a start, you have them back to front.
Both the examples as given will cause an error.
This way works for either method:
a=1
IF A = 1 THEN GOSUB THatONE
print p

IF A = 1 THEN THisONE
print p

end

SUB THISONE
P = A + 5
END SUB


THATONE:
P = A + 6
RETURN


The choice of gosub/return
sub/end sub or
function/end function
is up to you.
subs and functions allow local variables and tend to make more 'portable' code.

Functions are my preferred choice provided I only want to return one value.

Jim


VK7JH
MMedit
 
palcal

Guru

Joined: 12/10/2011
Location: Australia
Posts: 1965
Posted: 02:06pm 30 Jun 2014
Copy link to clipboard 
Print this post

The way I see it your first example should be

IF A = 1 THEN GOSUB THISONE

THISONE:
P = A+5
RETURN

and the new way

IF A = 1 THEN THATONE

Sub THATONE
P = A+5
EndSub

Paul.
"It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all"
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:41pm 30 Jun 2014
Copy link to clipboard 
Print this post

Thanks for the replies....

I guess I assumed gosub would work with sub, end sub. But now I get it. GOSUB works with LABLE: RETURN.

I was going over the meter clock code from Jman (which is really awesome code) and noticed he used both methods throughout the program, but I cannot see why one was used over the other in any of part of the code. Does one have a benefit over the other?
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:51pm 30 Jun 2014
Copy link to clipboard 
Print this post

Towards the end of the meter clock code...

' Convert to Hex
Sub BCDtoHex TempDec
hex = Fix(tempdec / 10) * 16
hex = hex Or ((tempdec / 10) - (Fix(tempdec / 10))) * 10
Return

ReadSettings:
i2caddr = &H50 ' 24c32 address
I2C Open 100,100
I2C Write i2caddr, 0,2,0,0 '24C32 Start Location
Pause 20
I2C Read i2caddr, 0,1,Brightness
I2C Close
Return

The "Sub BCDtoHex TempDec" has a "Return" instead of a End Sub and the next one has
"ReadSettings:" with a Return, so I am getting confused now.
 
jman

Guru

Joined: 12/06/2011
Location: New Zealand
Posts: 711
Posted: 07:14pm 30 Jun 2014
Copy link to clipboard 
Print this post

  viscomjim said   Towards the end of the meter clock code...

' Convert to Hex
Sub BCDtoHex TempDec
hex = Fix(tempdec / 10) * 16
hex = hex Or ((tempdec / 10) - (Fix(tempdec / 10))) * 10
Return



I do believe you found a bug on my part
this should be a End Sub
I seem to have not updated the posted code (My code in the Meter Clock is correct)

The next one ReadSettings:
is a normal gosub so the return is correct

Jman


 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 08:45pm 30 Jun 2014
Copy link to clipboard 
Print this post

In my opinion the GOSUB/RETURN/IRETURN should be deprecated and never be used in newly written software.
It has only value for backward compatibility. Never use it.

Microblocks. Build with logic.
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 10:38pm 30 Jun 2014
Copy link to clipboard 
Print this post

That sounds like good advice TZ. Like viscomjim, I've tied myself up in the past with exactly this issue and never known exactly which way it should be used.

I remember trying to figure it out using the MMBasic Manual but the difference is not really spelled out there. I reckon it would be worth Geoff adding a small paragraph (with an example) to do that.

Greg
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 01:37am 01 Jul 2014
Copy link to clipboard 
Print this post

Thank you for your help. It sounds like I should stick to SUB... END SUB.
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 03:49pm 01 Jul 2014
Copy link to clipboard 
Print this post

Well, what do you know Jman,

This seems to work...

a=1
IF A = 1 THEN GOSUB THatONE
print p

IF A = 1 THEN THisONE
print p

end

SUB THISONE
P = A + 5
RETURN


THATONE:
P = A + 6
RETURN

RETURN works for both of them.... This is what I get when run on uMite...

RUN
7
6
>

So your code seems to be ok with uMite.... This is probably why there is no problem with your code for the meterclock. All is well in uMite land.


Edited by viscomjim 2014-07-03
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 03:58pm 01 Jul 2014
Copy link to clipboard 
Print this post

Weirdness... This works also....

a=1
IF A = 1 THEN GOSUB THatONE
print p

IF A = 1 THEN THisONE
print p

end

SUB THISONE
P = A + 5
end sub


THATONE:
P = A + 6
end sub


When run on uMite I get...

RUN
7
6
>

Seems like they both work.... return and end sub!!!

Edited by viscomjim 2014-07-03
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6220
Posted: 05:31pm 01 Jul 2014
Copy link to clipboard 
Print this post

They might work now but future versions of MMBasic might tighten up on the syntax.
I would strongly recommend sticking to the 'correct' forms.

Jim
VK7JH
MMedit
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 10:31pm 01 Jul 2014
Copy link to clipboard 
Print this post

So the "correct" form for subroutines is

If a = 1 then Thisone

code

end

sub Thisone
code
end sub

I noticed in the uMite manual, when Geoff is explaining local variables on page 29, he uses the lable: return type subroutine. Does this have anything to do with local variables perhaps?

 
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