Log In  
Log In  

I'm trying to write a function to switch negative to positive and vise-versa. I've got:


function switch(f)

when I call switch(foot) it returns the original value but if at the command prompt I type "foot*=-1 it preforms as expected.

Thanks for any help and sorry for these remedial questions.


P#41424 2017-06-09 01:49 ( Edited 2017-06-12 23:25)

:: NeithR

Try this:


function switch(f)
 return f*-1


P#41425 2017-06-09 02:29 ( Edited 2017-06-09 06:29)
:: ianh

You can also write -foot without any multiplication.

P#41428 2017-06-09 02:42 ( Edited 2017-06-09 06:42)

Why not just:

foot = 3

and to "switch":

foot = -foot
P#41429 2017-06-09 02:44 ( Edited 2017-06-09 06:44)
:: xyzzy

Thanks for all the feedback, I'm still unclear as to why my original snippet didn't work. I was under the impression that pico8(lua) was a "pass by reference" language. Here is a non math version that also doesn't do what I expect it to:


function switch(f)



returns foo

P#41495 2017-06-09 15:53 ( Edited 2017-06-09 19:54)
:: JTE

Strings and numbers are not passed as a reference, no. Only tables are. Strings aren't even moddable in-place, you can only construct new string values.

P#41498 2017-06-09 16:28 ( Edited 2017-06-09 20:28)
:: xyzzy

ahh, thank you @JTE

P#41499 2017-06-09 17:11 ( Edited 2017-06-09 21:11)

Its a tricky but important distinction, xyzzy. In pretty much any language, saying a = x makes 'a' hold the contents of 'x', whether x is a reference or a bare value.

a = 1
b = a
b = 2
-- a is still 1 now

This code evaluates '1', and puts that value or reference in a. Then, it evaluates 'a', and puts either the value 1 or a reference to 1 in 'b'. So when we change what a was, b is unaffected.

P#41570 2017-06-12 19:25 ( Edited 2017-06-12 23:25)

[Please log in to post a comment]

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-03-25 03:28 | 0.017s | 4194k | Q:27