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

Get help with using AutoHotkey and its commands and hotkeys
User avatar
RobertL
Posts: 545
Joined: 18 Jan 2014, 01:14
Location: China

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

Post by RobertL » 16 Jun 2021, 21:00

[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.
Last edited by RobertL on 17 Jun 2021, 19:10, edited 1 time in total.
我为人人,人人为己?

swagfag
Posts: 4663
Joined: 11 Jan 2017, 17:59

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

Post by swagfag » 17 Jun 2021, 15:07

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

User avatar
RobertL
Posts: 545
Joined: 18 Jan 2014, 01:14
Location: China

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

Post by RobertL » 17 Jun 2021, 19:10

Thanks, @swagfag
I saw ByRef, but miss the Optional Parameters.
I think there should be a related link between them.
我为人人,人人为己?


lexikos
Posts: 7574
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

Post by lexikos » 20 Jun 2021, 04:57

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

swagfag
Posts: 4663
Joined: 11 Jan 2017, 17:59

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

Post by swagfag » 20 Jun 2021, 05:18

it was just a formatting edit iirc

lexikos
Posts: 7574
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

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

Post by lexikos » 20 Jun 2021, 05:24

In that case, never mind.

Post Reply

Return to “Ask For Help”