Jump to content

How to pass a local function variable back out?


GeorgeB
 Share

Recommended Posts

How would I call a function from my main script, have that function assign a value to a local function variable, and then once the function ends, pass that variable back outside the function? I would prefer not to use global variables.

I have looked all over AutoIT help, as well as doing Google searches, but I just can't seem to find any examples of how to do this! Maybe I'm blind? :x

Thanks in advance for any help!

Link to comment
Share on other sites

I am still pretty new with AutoIT and I just can't get this to work.

To provide an example of exactly what I am asking help on, I wrote a very small script below in this post.. All this does is call a function, in which a variable is set, then output to the screen. Then another function is called where another variable is set, and it too then is output to the screen.

(I only included the screen output commands in each function to ensure the variables are set by displaying them. They are not needed at all for either this example, or for my own script.

What changes to the script shown below would I need to do in order to be able to keep the value of $var1 and $var2 outside of their respective functions? Every example I've found in AutoIT's help, or in Google searches just do not really explain this at all.

Thanks all for any help! Sorry for the newbie question, I am sure for an AutoIT pro, this is a really simple question!

Sample code:

===============================================

_Function1()

_Function2()

func _Function1()

$var1 = "Hello"

MsgBox(8192, "_Function1 output", $var1)

EndFunc

Func _Function2()

$var2 = "Goodbye"

MsgBox(8192, "_Function2 output", $var2)

EndFunc

===============================================

Link to comment
Share on other sites

You can also pass it ByRef as a parameter.

Func BurnItWithFire(ByRef $it)
    $it = "burned"
EndFunc

; elsewhere
Dim $myvariable
BurnItWithFire($myvariable)
; $myvariable now equals "burned"

Richard, thanks!!! That answered my question perfectly! I hadn't noticed your answer had appeared before I wrote my next e-mail with the example. But never mind that now, your example was perfect and it answered the question!

Thank you, and Happy Holidays!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...