Function arguments as variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NewbieITguy
Posts: 8
Joined: 21 Jun 2021, 09:49

Function arguments as variables

24 Jun 2021, 08:44

First off, I'd like to thank everyone here for being so helpful. I'm always bugging you folks.

Anyway, I'm about to make a function, I wanted to make it so that the argument that's in the function could be used to create a variable.

In other words:

Code: Select all

^r::
cat := ""
VarSave(%cat%)
send "%cat%"
return



VarSave(x)
	{
	x = %x%
	%x% :=2
	return %x% ; Now variable cat should exist and equal 2
	}
As you can see, I'm goofing up big-time. I guess I'm basically asking how you pass a variable as an argument and then let said function edit whatever that variable is.
gregster
Posts: 9014
Joined: 30 Sep 2013, 06:48

Re: Function arguments as variables

24 Jun 2021, 10:53

Several problems. Syntax is one, another one is that a function is a "black box" with local variables (at least by default). But you could do this:

Code: Select all

^r::
; cat := ""
VarSave(cat)
msgbox %cat%
return

VarSave(ByRef x)
{
	x := 2
}
See https://www.autohotkey.com/docs/Functions.htm#ByRef
NewbieITguy
Posts: 8
Joined: 21 Jun 2021, 09:49

Re: Function arguments as variables

24 Jun 2021, 11:16

gregster wrote:
24 Jun 2021, 10:53
Several problems. Syntax is one, another one is that a function is a "black box" with local variables (at least by default). But you could do this:

Code: Select all

^r::
; cat := ""
VarSave(cat)
msgbox %cat%
return

VarSave(ByRef x)
{
	x := 2
}
See https://www.autohotkey.com/docs/Functions.htm#ByRef
I'll try this soon, thanks!

Real-talk, should I just sit down one day and read the entire doc for AHK? Is that what everyone else did?
gregster
Posts: 9014
Joined: 30 Sep 2013, 06:48

Re: Function arguments as variables

24 Jun 2021, 11:25

Perhaps not in one day ;) Many things you don't need to know, just where to find them... :) Btw, the limited scope of functions is something that you will encounter in most (all ? :think: ) programming languages. Other things are quite specific to AHK. I would probably start with the Usage and Syntax section of the docs.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Function arguments as variables

24 Jun 2021, 11:31

yes, u should read the entire docs. read, not understand, not memorize. u need to know what things exist. then u at least would have a starting point for looking up stuff u dont quite understand

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 279 guests