![]() |
Forum Index : Microcontroller and PC projects : MMBasic subroutine parameters
Author | Message | ||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1638 |
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. Bill Keep safe. Live long and prosper. |
||||
Pluto Guru ![]() Joined: 09/06/2017 Location: FinlandPosts: 375 |
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: AustraliaPosts: 1638 |
Well done Fred, ![]() ![]() 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: FinlandPosts: 375 |
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: AustraliaPosts: 3282 |
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 KingdomPosts: 2170 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |