Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:07 06 Jul 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 subroutine parameters

Author Message
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1638
Posted: 04:30am 12 Jan 2022
Copy link to clipboard 
Print this post

This may not be of interest to anyone but..

While I was reading the manual the other day, it struck me that subroutine parameters are treated in the same way as locals. They exist only within the subroutine and disappear when the subroutine returns. Also, when a subroutine is called, any parameters missing in the call are set to zero or null as appropriate.

So why not use an extra parameter as a local. It saves an extra line, probably quicker, but not only that it works!

It could be used by someone wanting to write a short and/or obtuse program or perhaps to save memory?

I Expect unused parameters in functions will behave the same way.

  Quote  ' Subroutine parameter test

X =
5 : ? "x = "; : ? x
y =
4 : ? "y = "; : ? y

Swap x, y

? : ?
"now:"

?
"x = "; : ? x
?
"y = "; : ? y

Xch x, y

? : ?
"return them:"

?
"x = "; : ? x
?
"y = "; : ? y

SUB Swap a, b
 
LOCAL t
 t = a
 a = b
 b = t
END SUB

SUB Xch a, b, t
 t = a
 a = b
 b = t
END SUB


Bill
Keep safe. Live long and prosper.
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 375
Posted: 07:00am 12 Jan 2022
Copy link to clipboard 
Print this post

And if the variable type is different in MAIN and SUB, the the result is different.

' Subroutine parameter test
Dim As FLOAT x,y  '!!!!!!! FLOAT

X = 5 : Print "x = "; : Print x
y = 4 : Print "y = "; : Print y

Swap x, y

Print : Print "now:"

Print "x = "; : Print x
Print "y = "; : Print y

Xch x, y

Print : Print "return them:"

Print "x = "; : Print x
Print "y = "; : Print y

Sub Swap a As integer, b As integer    '!!!!! INTEGER
 Local t
 t = a
a = b
 b = t
End Sub

Sub Xch a As integer, b As integer, t As integer
 t = a
 a = b
 b = t
End Sub

RUN
x =  5
y =  4

now:
x =  5
y =  4

return them:
x =  5
y =  4
>



/Fred
 
Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1638
Posted: 07:41am 12 Jan 2022
Copy link to clipboard 
Print this post

Well done Fred,    But I didn't say it was idiot proof. Of course if you do something stupid you make any program produce errors. I'm an expert at that.  

The Swap subroutine is a copy and paste from Geoff's manual. My version is to just test a concept.

Do the same using the Swap subroutine twice.

Please explain why it is invalid to use one or the other.

Bill
Keep safe. Live long and prosper.
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 375
Posted: 09:44am 12 Jan 2022
Copy link to clipboard 
Print this post

I hope you do not expect me to explain...
I have a long career of producing errors. At least from some of them I did learn something.
/Fred
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3282
Posted: 09:55am 12 Jan 2022
Copy link to clipboard 
Print this post

This could be the start of our very own Obfuscated BASIC Contest (sort of like the International Obfuscated C Code Contest).

Geoff
Geoff Graham - http://geoffg.net
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 10:11am 12 Jan 2022
Copy link to clipboard 
Print this post

  Turbo46 said  This may not be of interest to anyone but..


Hmmm... very interesting. I have a bit of an obsession with "optimising" and stuff like this scratches that itch for me.

Another thing I sometimes do in time critical Subs is to use globally defined variables as temporary storage (what might ordinarily be Locals). Of course you need to pay attention that bits of your code aren't trampling over each others work but it can give a speed boost in tight loops.

I will play. Thanks
Edited 2022-01-12 21:53 by CaptainBoing
 
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