Function to set a local variable

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Function to set a local variable

Post by kaveh1000 » Mon Dec 30, 2019 10:44 pm

Hi folks. I might be missing something very simple. When I use a function, normally I return a value that is then used locally. For example

Code: Select all

put square_it(6) into tSquare

function square_it pNumber
  return pNumber * pNumber
end square_it
tSquare now has value 36.

Now what I am looking for is to say:

Code: Select all

get square_it(6, tSquare)

function square_it pNumber, pVariable
...
end square_it
After calling it, the local value, tSquare, should automatically have the value 36. Assume it has not been declared as global, local, or script local.

Any ideas?
Kaveh

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10055
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Function to set a local variable

Post by FourthWorld » Mon Dec 30, 2019 10:50 pm

See the "@" operator in the Dictionary for handling arguments by reference, e.g.:

Code: Select all

function square_it pNumber, @pVariable 
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14206
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Function to set a local variable

Post by Klaus » Mon Dec 30, 2019 10:56 pm

Hi Kaveh,
Any ideas?
Yes, do not use a function but a handler and do not use variables, they HAVE to be declared somehow and somewhere!
Use a custom property instead like this:

Code: Select all

command square_it pNumber
   set the cNameThisPropertyAsYouLike of this stack to pNumber*pNumber
end square_it
But maybe I do not really understand what you are looking for... :D


Best

Klaus

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Function to set a local variable

Post by kaveh1000 » Mon Dec 30, 2019 11:30 pm

Thank you both for the fast replies. Richard, you got it. I had no idea about this but it works! Thank you so much.

And Klaus thanks for reminder about custom properties which have saved the day many times!
Kaveh

Post Reply