![]() |
Forum Index : Microcontroller and PC projects : MMBasic Subroutines
Author | Message | ||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
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 ZealandPosts: 9486 |
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!!!! ![]() Smoke makes things work. When the smoke gets out, it stops! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6220 |
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: AustraliaPosts: 1965 |
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 StatesPosts: 925 |
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 StatesPosts: 925 |
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 ZealandPosts: 711 |
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: ThailandPosts: 2209 |
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: AustraliaPosts: 1329 |
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 StatesPosts: 925 |
Thank you for your help. It sounds like I should stick to SUB... END SUB. |
||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
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. |
||||
viscomjim Guru ![]() Joined: 08/01/2014 Location: United StatesPosts: 925 |
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!!! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6220 |
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 StatesPosts: 925 |
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? |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |