what does ByRef do? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bigdeal
Posts: 66
Joined: 13 Feb 2017, 06:31

what does ByRef do?

09 Apr 2017, 13:48

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
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: what does ByRef do?

09 Apr 2017, 14:31

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
and:

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
}
do basically the same.

Hope this helps.
Last edited by A_AhkUser on 09 Apr 2017, 14:41, edited 1 time in total.
my scripts
bigdeal
Posts: 66
Joined: 13 Feb 2017, 06:31

Re: what does ByRef do?

09 Apr 2017, 14:41

OOOOOOOOOOOO so it allows you to use that variable in the function, got it thanks alot man
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: what does ByRef do?  Topic is solved

09 Apr 2017, 15:13

bigdeal wrote:OOOOOOOOOOOO so it allows you to use that variable in the function
Yes but note it is also the defnition of the global keywords:

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
In fact, one of the main advantage of using ByRef is that it allows send back extra results since return can send back only one value.

Code: Select all

test := ""

MyFunc(ByRef __param) {
global test := "bonjour"
__param := 100*3
return 7
}

MsgBox % MyFunc(var) . "," .  test . "," . var ; returns 7,bonjour,300
See also documentation now you got it for other case where it is advantageous to use ByRef parameters.
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, peter_ahk, Rauvagol, Spawnova, wineguy and 287 guests