Jump to content
Sign in to follow this  
Dragon5150

User defined function question, persistant vs non-persitant varaibles

Recommended Posts

Dragon5150

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

Share this post


Link to post
Share on other sites
dbzfanatic

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.

Share this post


Link to post
Share on other sites
Dragon5150

So let me try this,

If I dim the variable outside of a function it will be globally available, but it I dim it inside the function it should not be accessible once the function has returned?

Share this post


Link to post
Share on other sites
dbzfanatic

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.

Share this post


Link to post
Share on other sites
Dragon5150

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.

Share this post


Link to post
Share on other sites
LarryDalooza

In general I am not too picky about my local variable names...

Func example($szIamLocal, $szIamLocalAlso)
   Local $varLocal
   ;...
EndFuncoÝ÷ Ù»­½êò¦'$Ë^v¦x Úö«¦åzÆ®¶­sdvÆö&Âb33c¶uôÕ7V6ÅvF×uTäDU%44õ$

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.