The reason to use "Return (%Var%)" Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

The reason to use "Return (%Var%)"

22 Jul 2019, 08:25

From the https://www.autohotkey.com/docs/commands/Return.htm:
Known limitation: For backward compatibility and ease-of-use, the following two examples are functionally identical:

* return MyVar
* return %MyVar%

In other words, a single variable enclosed in percent signs is treated as a non-expression. To work around this, make it unambiguously an expression by enclosing it in parentheses; for example: return (%MyVar%).
Can anybody explain the last part of this quote ("In other words..."). I don't understand which problems we are trying to "work around". And my attemts to use something like (%MyVar%) doesn't gave me any good results.

Code: Select all

Test() {
    Var := "Foo"

    ; return Var     - returns "Foo"
    ; return %Var%   - the same
    ; return (%Var%) - returns empty string. Doesn't make sense for me
    return (%Var%)
}

MsgBox, % Test()
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: The reason to use "Return (%Var%)"  Topic is solved

22 Jul 2019, 08:41

Usually, if you use %var% while already in an expression, it will do a double-deref, and get the contents of the variable whose name is stored in var (in your case, it would look for the variable named Foo ).

Code: Select all

Test() {
    Var := "Foo"
	Foo := "Bar"
    ; return Var     - returns "Foo"
    ; return %Var%   - the same
    ; return (%Var%) - returns "Bar"
    return (%Var%)
}

MsgBox, % Test()
Edit:
btw
https://www.autohotkey.com/docs/Functions.htm#More_about_locals_and_globals wrote:Within a function (unless force-local mode is in effect), any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists.
So, this works, too:

Code: Select all

Test() {
    Var := "Foo"
	; return Var     - returns "Foo"
    ; return %Var%   - the same
    ; return (%Var%) - returns "Bar"
    return (%Var%)
}
Foo := "Bar"
MsgBox, % Test()
(local variable takes precedence, though, if it exists)
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: The reason to use "Return (%Var%)"

16 Oct 2020, 05:43

To compare the example by Gregster with cases with no return:

Code: Select all

Var := "Foo"
Foo := "Bar"
msgbox % %Var%    ; => "Bar"
msgbox % (%Var%)  ; => "Bar"

Code: Select all

Var := "Foo"
Foo := "Bar"
; Abc := %Var%
; Abc := (%Var%)
msgbox % Abc  ; => "Bar"
User avatar
lmstearn
Posts: 694
Joined: 11 Aug 2016, 02:32
Contact:

Re: The reason to use "Return (%Var%)"

26 Sep 2021, 09:27

Strget also helps to illustrate the effect:

Code: Select all

Test() {
    Var := "Foo"
    ; return Var     - returns "Foo"
    ; return %Var%   - the same
    ; return (%Var%) - returns "Bar"
    return Strget(&%Var%)
}
Foo := "Bar"
MsgBox, % Test()
; 
Var := "Foo"
Foo := "Bar"
Abc := Strget(&%Var%)
msgbox % Abc  ; => "Bar"
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, jameswrightesq, Lem2001 and 406 guests