AutoHotkey Community

It is currently May 27th, 2012, 5:24 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: February 7th, 2011, 5:15 pm 
I'm having trouble understanding how parameters are accessed in AutoHotKey functions.

For example, I set myVar variable with the InputBox, then pass it to a function. How do I evaluate the arg in the TestFunction?

#t::
inputbox myVar, What is your variable?
myNewVar := TestFunction(%myVar%)
MsgBox %myNewVar%
return

TestFunction(arg)
{
MsgBox arg
msgBox %arg%
return %arg%
}

Thanks!

Chris


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2011, 5:19 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Get rid of the %%
Code:
myNewVar := TestFunction(myVar)


Functions
http://www.autohotkey.com/docs/Functions.htm

Tutorial:
http://www.autohotkey.net/~deleyd/xprxm ... amples.htm

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2011, 5:21 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
Next time please place your code in [code][/code] tags!

With regards to passing arguments in functions:
Code:
inputbox myVar, What is your variable?
myNewVar := TestFunction( myVar ) ; do not use percents for parameters.
MsgBox %myNewVar%
return

TestFunction( arg )
{
  msgBox %arg% ; var scope is limited to the function unless made global.
  return arg ; use 'return' to return a value or string to its caller!
}
htms

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Last edited by TLM on February 7th, 2011, 11:31 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2011, 11:20 pm 
Offline

Joined: July 25th, 2006, 9:06 am
Posts: 51
As an extension to this, how does one pass the name of the variable to a function?
I wanted to have a Debug function that printed the name of the variable and the value in it. I have searched the forum a bit but have not found what I need.

Peterm


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2011, 11:53 pm 
Offline

Joined: August 21st, 2006, 7:07 pm
Posts: 2925
Location: The Shell
There may be another way but the easiest would be just to send the var name to the function as a parameter.
Code:
numberVar := TestFunction( 1234, "numberVar" )
msgbox % numberVar

TestFunction( arg, var="" ) {
  return "Value: " arg "`nVariable Name: " var
}

Hard, and not 100% perfect method
Code:
numberVar := TestFunction( 1234, a_lineNumber ) ; the a_lineNumber parameter is essential or the function will fail.
msgbox % numberVar

TestFunction( arg, Ln ) {
  fileReadLine, vLn, % a_scriptFullPath, % Ln
  return "Value: " arg "`nVariable Name: " regexReplace( vLn, "[\s:=]+[\w(),;.""""]+" )
}
There maybe some extra patterns that can sneak their way past the regexReplace
( sorry I'm new to rE ) ;)

_________________
Imageparadigm.shift:=(•_•)┌П┐RTFM||^.*∞


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2011, 10:33 pm 
Offline

Joined: July 25th, 2006, 9:06 am
Posts: 51
Thanks for reply but first means one has to enter the variable in name in the function call.
The second is an interesting idea but tricky to make 100% correct.
I rather hoped thare was a global variable set up with the current variable name.

Peterm


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot], HotkeyStick, rbrtryn, XstatyK, Yahoo [Bot] and 84 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group