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: Passing variables by value - sneaky trick
Author | Message | ||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8516 |
If you want to pass a variable by value and be certain it won't get altered in a function or subroutine just enclose it in brackets in the calling sequence. This forces the interpreter to create a temporary variable in order to evaluate the contents of the bracket and it is then the temporary variable that is passed. Edited 2020-09-23 00:00 by matherp |
||||
Nimue Guru Joined: 06/08/2020 Location: United KingdomPosts: 367 |
So - this works a treat. Options: Defining a local variable in the function is more verbose but obvious Passing ((x)) is "easier" but less transparent. In teaching this to kids -- I think the more verbose way is clearer.... Is either way more "efficient" as far as the interpreter is concerned? Nim Entropy is not what it used to be |
||||
Nimue Guru Joined: 06/08/2020 Location: United KingdomPosts: 367 |
I suppose in a way this brings us full circle to the thread on "reusable code" --- the ((X)) method is great -- but if I write a library to do the binary conversion -- the "make a local copy" way is never wrong...... unless someone points out that I'm wrong (again). Cheers Nim Entropy is not what it used to be |
||||
vegipete Guru Joined: 29/01/2013 Location: CanadaPosts: 1081 |
That makes more sense than passing "x+0" or "x*1" Visit Vegipete's *Mite Library for cool programs. |
||||
William Leue Guru Joined: 03/07/2020 Location: United StatesPosts: 379 |
Neat trick! In passing, I also have found that, although MMBasic does not have real boolean variables, an ordinary integer variable can act like a boolean: b = 0 if b then print "b is true" else print "b is false" b = 1 if b then print "b is true" else print "b is false" end this produces: b is false b is true -Bill |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 369 |
[ warning - killjoy mode ] tut tut ! I think that should invoke a variable called 7...yada yada yada...and exasperate users... Nifty though. |
||||
mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
Intersting... Yeah I"ll definitely add that to the document that I've been working on! Too bad it's on the caller side to do it, but it's still an interesting trick. |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3641 |
hmm, in almost no programming languages (and of course not in mathematics). John |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 369 |
I have heard the opinion -in pass-by-reference dogma-world - that subroutine calls should not accept numerical values at all eg CALL foo(7) should generate an error. (It did seem a bit idiotic..but then I.T. has had a few moments..) |
||||
Sasquatch Senior Member Joined: 08/05/2020 Location: United StatesPosts: 296 |
You can also use the NOT operator: If NOT b then print "b is false" -Carl |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8516 |
Makes some sort of sense but does the same dogma preclude passing a function? I assume it would have to. |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 369 |
that had to be declared external I think |
||||
Print this page |