Page 1 of 1

[close] recursive ByRef with empty/default input wont pass correct parameter

Posted: 16 Jun 2021, 21:00
by RobertL
[Moderator's note: Topic moved from Bug Reports.]

Code: Select all

f(ByRef p:=""){
	if not p	;endless loop here
		p:=0
	if(p++<2)
		f(p)
	return p
}
MsgBox % f()
Functions from help document
Although a function may call itself recursively, if it passes one of its own local variables or non-ByRef parameters to itself ByRef, the new layer's ByRef parameter will refer to its own local variable of that name rather than the previous layer's. However, this issue does not occur when a function passes to itself a global variable, static variable, or ByRef parameter.

Re: recursive ByRef with empty/default input wont pass correct parameter

Posted: 17 Jun 2021, 15:07
by swagfag
https://www.autohotkey.com/docs/Functions.htm#OptionalByRef
[v1.0.46.13+]: ByRef parameters also support default values; for example: MyFunc(ByRef p1 = ""). Whenever the caller omits such a parameter, the function creates a local variable to contain the default value; in other words, the function behaves as though the keyword "ByRef" is absent.
this coupled with the limitation u have already posted explains why it isnt working

Re: recursive ByRef with empty/default input wont pass correct parameter

Posted: 17 Jun 2021, 19:10
by RobertL
Thanks, @swagfag
I saw ByRef, but miss the Optional Parameters.
I think there should be a related link between them.

Re: [close] recursive ByRef with empty/default input wont pass correct parameter

Posted: 18 Jun 2021, 04:16
by swagfag

Re: [close] recursive ByRef with empty/default input wont pass correct parameter

Posted: 20 Jun 2021, 04:57
by lexikos
@RobertL
Before editing your posts, consider how it will read for someone who has not seen the unedited version.

I have not seen the unedited version, and can only presume that it would have been less confusing than what you have now, with your first post containing a documentation quote which shows why this is not a bug, but nothing explaining why it is there or when it was added.

Re: [close] recursive ByRef with empty/default input wont pass correct parameter

Posted: 20 Jun 2021, 05:18
by swagfag
it was just a formatting edit iirc

Re: [close] recursive ByRef with empty/default input wont pass correct parameter

Posted: 20 Jun 2021, 05:24
by lexikos
In that case, never mind.