what does ByRef do? Topic is solved
what does ByRef do?
hey im newbie and im a little starting to learn ahk, so I read it and still don't understand what it does, can someone explain me quick, thanks in advance btw
Re: what does ByRef do?
Code: Select all
MyFunc(__param) {
; ...
}
MyFunc(test)
MsgBox % test ; return blank not defined outside the scope of the function
;----------------------------------------
MyFunc(ByRef __param) {
__param := "blabla"
}
MyFunc(test)
MsgBox % test ; return blabla since the use of ByRef causes each parameter to become an alias for the variable passed in from the caller
EDIT*:
You can compare a byrefparameter to the 'outpuvar...' paramater of MouseGetPos command, for exemple:
Code: Select all
MouseGetPos, OutputVarX, OutputVarY, OutputVarWin, OutputVarControl
MsgBox % "the ID of the window under the mous is " . OutputVarWin
Code: Select all
MouseGetPos(OutputVarX, OutputVarY, OutputVarWin, OutputVarControl)
MsgBox % "the ID of the window under the mous is " . OutputVarWin
MouseGetPos(ByRef OutputVarX: "", ByRef OutputVarY:="", ByRef OutputVarWin:="", ByRef OutputVarControl:="", Mode:="") {
MouseGetPos, OutputVarX, OutputVarY, OutputVarWin, OutputVarControl, % Mode
}
Hope this helps.
Last edited by A_AhkUser on 09 Apr 2017, 14:41, edited 1 time in total.
Re: what does ByRef do?
OOOOOOOOOOOO so it allows you to use that variable in the function, got it thanks alot man
Re: what does ByRef do? Topic is solved
Yes but note it is also the defnition of the global keywords:bigdeal wrote:OOOOOOOOOOOO so it allows you to use that variable in the function
Code: Select all
test := ""
MyFunc() {
global test ; make test -- create outside the function -- accessible within the scope of the function -- where variable are all impicitly locals -- declaring it as global (see: https://autohotkey.com/docs/Functions.htm#Locals)
test := "bonjour"
}
MyFunc()
MsgBox % test ; returns bonjour
Code: Select all
test := ""
MyFunc(ByRef __param) {
global test := "bonjour"
__param := 100*3
return 7
}
MsgBox % MyFunc(var) . "," . test . "," . var ; returns 7,bonjour,300
Who is online
Users browsing this forum: Bing [Bot], peter_ahk and 350 guests