Jump to content

User defined function question, persistant vs non-persitant varaibles


Recommended Posts

I am new to AutoIT but not to scripting and programing in general. When I used Winbatch there were two distinct type of user defined options, you had user defined functions and user defined subroutines.

The major difference being a subroutine had access to all variables in the program and a function only could see its localized variables.

Is there the same functionality in AutoIT? All I see are functions that can see all variables and do not terminate when the function is ended.

Also,

there was drop and dropwild commands to release variables in Winbatch, is there such a command in AutoIT?

Any help would be greatly appreciated and I look forward to being able to contribute to this board.

Adam Franks

adam (dot) franks (at) gmail (dot) com

Link to comment
Share on other sites

I don't think there's a command to remove a variable but I could be wrong. Functions can see local and global variables but if a variable is simply Dim-ed (in my experience) the function can use them but the values are not held once the function is over.

Link to comment
Share on other sites

i guess it was a bug in my coding at the time. Just tried an example and the function does indeed modify the variable outside itself but only if you use the same variable. So

Dim $var, $one

While 1
Sleep(250)
_Func($var)
MsgBox(0,"",$var)
WEnd

Func _Func($int)
If $one = 0 Then
$var = $int + 3
$one += 1
Endif
EndFunc

would show a message box with "3" as the text. If you were to do something like this

Dim $var, $one

While 1
Sleep(250)
MsgBox(0,"",_Func($var))
WEnd

Func _Func($int)
Dim $ret
If $one = 0 Then
$ret = $int + 3
$one += 1
Endif
Return $ret
EndFunc

It would return 3 once then return an empty string because the variable $ret is being redimmed with a value of 0.

Link to comment
Share on other sites

i guess it was a bug in my coding at the time. Just tried an example and the function does indeed modify the variable outside itself but only if you use the same variable. So

Dim $var, $one

While 1
Sleep(250)
_Func($var)
MsgBox(0,"",$var)
WEnd

Func _Func($int)
If $one = 0 Then
$var = $int + 3
$one += 1
Endif
EndFunc

would show a message box with "3" as the text. If you were to do something like this

Dim $var, $one

While 1
Sleep(250)
MsgBox(0,"",_Func($var))
WEnd

Func _Func($int)
Dim $ret
If $one = 0 Then
$ret = $int + 3
$one += 1
Endif
Return $ret
EndFunc

It would return 3 once then return an empty string because the variable $ret is being redimmed with a value of 0.

Thanks for explaining it. I will have to go with using leading letters on the variables then to make sure I do not mess anything else up in the program,

such as

Funct _newfunct( $drg_newvar )

$drg_nextvar

EndFunc

the drg_ being the unique part of everything inside of that function.

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