Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 06:00 29 Mar 2024 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: Passing variables by value - sneaky trick

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8516
Posted: 01:58pm 22 Sep 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 367
Posted: 02:13pm 22 Sep 2020
Copy link to clipboard 
Print this post

  matherp said  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.



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 Kingdom
Posts: 367
Posted: 02:17pm 22 Sep 2020
Copy link to clipboard 
Print this post

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: Canada
Posts: 1081
Posted: 06:08pm 22 Sep 2020
Copy link to clipboard 
Print this post

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 States
Posts: 379
Posted: 12:22pm 23 Sep 2020
Copy link to clipboard 
Print this post

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: Australia
Posts: 369
Posted: 01:03pm 23 Sep 2020
Copy link to clipboard 
Print this post

[ 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 States
Posts: 261
Posted: 01:05pm 23 Sep 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 3641
Posted: 01:45pm 24 Sep 2020
Copy link to clipboard 
Print this post

  zeitfest said  [ warning - killjoy mode ]
I think that should invoke a variable called 7...yada yada yada...and exasperate users...

hmm, in almost no programming languages (and of course not in mathematics).

John
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 369
Posted: 02:39pm 25 Sep 2020
Copy link to clipboard 
Print this post

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 States
Posts: 296
Posted: 03:43pm 25 Sep 2020
Copy link to clipboard 
Print this post

  William Leue said  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


You can also use the NOT operator:

If NOT b then print "b is false"
-Carl
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8516
Posted: 03:54pm 25 Sep 2020
Copy link to clipboard 
Print this post

  Quote  I have heard the opinion -in pass-by-reference dogma-world - that subroutine calls  should not accept numerical values at all


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: Australia
Posts: 369
Posted: 04:48am 26 Sep 2020
Copy link to clipboard 
Print this post

  Quote  passing a function

that had to be declared external I think
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024