Page 1 of 1

Function Parameters/Return Values

Posted: 23 Oct 2017, 09:20
by Delta Pythagorean
Disclaimer: In all honesty, I'm writing this at like 4 in the morning with like 2 McDonalds fries off to the side. If it seems I was lazy on something, please forgive me. Thanks.
Anyway:
Want to know how to use function's way of grabbing parameters and using them for more than just a bunch of singular variables?
Well, first you have to know a few things.

First things first, what a function's purpose is.
Second, why you shouldn't use Global in a function for using variables.
Third, why using arrays and objects is something you should start to lean towards when asking for parameters.
And finally, what to return, throw, and set when using functions.

Once you've seen the docs about a million times, now it's time to put that useless knowledge to use :D

First, make a function. Give it a name!... No not that name... Not that either...
Something like... MyFunction :D

Anyway. Now you get to decide what the function does, for the sake of your eyes reading so many words and for my fingers to have a rest, I'll just use an input function.

Code: Select all

InputBox(Title, Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default) {
	InputBox, Out, Title, Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default]
	Return Out
}
Looks kinda... long. Let's shorten than shall we?

Code: Select all

InputBox(Title, Prompt, Options := "") {
	HIDE := (Options.HasKey("Hide") && Options.Hide = True) ? "HIDE" : ""
	Width := (Options.HasKey("Size")) ? Options.Size[1] : ""
	Height := (Options.HasKey("Size")) ? Options.Size[2] : ""
	X := (Options.HasKey("Pos")) ? Options.Pos[1] : ""
	Y := (Options.HasKey("Pos")) ? Options.Pos[2] : ""
	Font := (Options.HasKey("Font")) ? Options.Font : ""
	Timeout := (Options.HasKey("Timeout")) ? Options.Timeout : ""
	Default := (Options.HasKey("Default")) ? Options.Default : ""
	InputBox, Out, Title, Prompt, % HIDE, % Width, % Height, % X, % Y, % Font, % Timeout, % Default
	Return Out
}
Looks kind of tall but it's at least easier to use and manipulate!
Wait, what's that? How does this work you may ask?

Well, the parameters you should already know. Options := "" Forces the parameter to be empty unless the parameter is given a value.

How do I call this function?
Like so:

Code: Select all

InputBox("Hello!", "Please input your password.", {Hide: True, Timeout: 10, Size: [300, 200]})
This calls the function to have the edit's input to be a password, the parameter "Size" calls for an array inside the object.
I have seen many people just use a ton of parameters for their functions, which isn't bad, but it is quite annoying and can take up a lot of space.

Moving on, let's get those pesky Return values out of the way.
First, the Return command is always in expression mode, so, Return, 1 + 2 would return 3. You can also return objects and arrays. Allowing users to retrive the data they want, not what is just returned.
You might be screaming USE BYREF, alright alright. I get it, but hear me out, ByRef should only be used on command-like functions, such as SplitPath and MouseGetPos.
HOWEVER, you can allow users to use objects with the function and return the data they want.
So:

Code: Select all

MouseGetPos(Options := 3) {
	MouseGetPos, X, Y, Win, Ctrl, % Options
	Return {X: X, Y: Y, Win: Win, Ctrl: Ctrl}
}
This allows the user to get X and Y separately by doing this:

Code: Select all

Data := MouseGetPos()
MsgBox, % Data.X " " Data.Y " " Data.Win " " Data.Ctrl
Making things a lot easier to track and use. Well... at least for me it is.

So in short, use these two new things to your advantage and tell the world that you learned something that will only just save you like... a minute of your time.
I implore you to think about this and use what I have just "taught" you to your advantage.

I hope this helps at all. To be honest, I shouldn't have done this. I need sleep...
Anyways. Thanks for reading this (If you didn't just skip through the text).

Re: Function Parameters/Return Values

Posted: 23 Oct 2017, 10:26
by derz00
Hey you missed some %'s. It should be

Code: Select all

InputBox, Out, % Title, % Prompt, % HIDE, % Width, % Height, % X, % Y, , % Timeout, % Default
When I add those, and took out the % Font your function works ;)

Edit: I think returning an object looks smarter than ByRef, nice example :thumbup:

Function Parameters/Return Values

Posted: 13 May 2018, 21:43
by andreysOdomo
is the return value of a function always the last data type before the function ends with ";;"

Re: Function Parameters/Return Values

Posted: 13 May 2018, 22:27
by gregster
andreysOdomo wrote:is the return value of a function always the last data type before the function ends with ";;"
Are you sure that you are looking for Autohotkey? It seems you are talking about other things (single ;s are only used for same-line comments in AHK, they don't have anything to do with functions specifically - functions usually don't end with ";;" here).

The return value of a function in AHK is the parameter directly following return in a function, which usually is an expression. From looking at this return value alone, you will often not be able to say which data type will be returned.

For the basics of the AutoHotkey language, please also look at the the tutorial, the 'Usage and Syntax' section of the docs and the specific help pages like return, with special focus on the examples.

Re: Function Parameters/Return Values

Posted: 08 Oct 2018, 11:21
by Guest
one of the episodes are playing. your tutorials are a great help to me, and I feel lost without access to further tutorials. any advice

Re: Function Parameters/Return Values

Posted: 10 Oct 2018, 15:38
by User
Delta Pythagorean wrote:.
You should have made it more, Straightforward!

Code: Select all

msgbox, % InputBox("Test", "Tester", {Input:"Hide", Width:300, Height:150, x:0, y:0, Default:"Type here"})


InputBox(Title, Prompt, o := "")	;__________________ InputBox(Function) - v1.0 ___________________
{
InputBox, Out, % Title, % Prompt, % o["Input"], % o["Width"], % o["Height"], % o["X"], % o["Y"], , % o["Timeout"], % o["Default"]
Return Out
}

Re: Function Parameters/Return Values

Posted: 10 Oct 2018, 17:11
by User
derz00 wrote:I think returning an object looks smarter than ByRef, nice example :thumbup:
Returning objects is ok, but, it still needs a variable!

I prefer to get values directly from functions, from their static variables, so I don't have to use "temp variables" to store "temp values"!

Using "temp variables" all the time is usually the source of a lot of problems in the main script!

Of course, like everything else in life, getting values directly from functions has its pros and cons, so, I tend to write functions with both options in mind! (so, it is up to who is using the functions to choose which option to use!)

Code: Select all

msgbox, `n`n Left Click anywhere! `n`n

~LButton::	;______ "~", keeps the "LButton" original function _______

Data := MouseGetPos()

ToolTip, % ""
. "Direct from Function: " MouseGetPos("Get", "x") "/" MouseGetPos("Get", "y") " - " MouseGetPos("Get", "Win") " - " MouseGetPos("Get", "ctrl")  "`n"
. "`n"
. "From returned Object: " Data.x "/" Data.y " - " Data.Win " - " Data.ctrl  "`n"
. "`n"
. "Press ''k'' to exit script!"

return

k::		;________ press "k" to exit script ________
exitapp


MouseGetPos(Options := 3, Var:= "")	;________________ MouseGetPos (Function) - v1.0 _________________
{
Static x, y, Win, Ctrl		;"Static" variables are always implicitly "local", but differ from "locals" variables because their values are remembered between calls.

if (Options = "Get")
return, (%Var%)			;return the function static variables values

MouseGetPos, X, Y, Win, Ctrl, % Options
Return {X: X, Y: Y, Win: Win, Ctrl: Ctrl}
}

Re: Function Parameters/Return Values

Posted: 10 Oct 2018, 23:44
by nnnik
I think this is a horrible practice.

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 00:08
by User
I think this is a FANTASTIC practice.

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 00:45
by nnnik
You should know that if you make a typo in that - then your function might return a variable from the global scope.

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 02:48
by swagfag
i think this is absolutely gnarly

Code: Select all

MouseGetPos("Get"...
having to tell a function that already has "get" in its name to go "get" something, just...

also i dont understand the case about temp vars. in ur function all those static vars are essentially temp vars. did u mean global vars?

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 05:33
by Delta Pythagorean
You should have made it more, Straightforward!

Code: Select all

MsgBox, % InputBox("Test", "Tester", {Input:"Hide", Width:300, Height:150, x:0, y:0, Default:"Type here"})

InputBox(Title, Prompt, o := "") {	;__________________ InputBox(Function) - v1.0 ___________________
	InputBox, Out, % Title, % Prompt, % o["Input"], % o["Width"], % o["Height"], % o["X"], % o["Y"], , % o["Timeout"], % o["Default"]
	Return Out
}
[/quote]
I agree with you, but my disclaimer should have given you an idea on what I was thinking :P
I do completely agree on using that layout then my towering, newbie-confusing, layout.

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 05:34
by Delta Pythagorean
nnnik wrote:You should know that if you make a typo in that - then your function might return a variable from the global scope.
What could you return that could give something from the global scope when nothing is called that is global besides some variable?

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 07:02
by nnnik

Code: Select all

someresul := 43

Msgbox % test("get", "someresul") ;shows 43

test(t := "",  r := "") {
	static someresult
	someresult := 42
	if (t == "get") {
		res := %r%
		return res
	}
}

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 07:34
by swagfag
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.
nasty

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 10:54
by User
nnnik wrote:You should know that if you make a typo in that - then your function might return a variable from the global scope.
Haha, so I should not use something because of typos and because of AutoHotkey no-sense glitches ...? Hehe, really nice!

Anyway, be careful about "typos" or use "Force Local" declaration inside functions (AHK 1.1.27+ only!) to avoid the glitch!

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 11:06
by User
swagfag wrote:having to tell a function that already has "get" in its name to go "get" something, just...
Haha, let me try to help you a little bit, MousePos(), then, MousePos("Get", ...) , Haha, you really made me Lol here! Hehe!
swagfag wrote:also i dont understand the case about temp vars. in ur function all those static vars are essentially temp vars. did u mean global vars?
What an absolutely gnarly question! I will not even tire myself to answer you this question of yours! Go read some documentations, it would be better for you ...!

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 17:45
by nnnik
Additionally you have an issue if another thread interrupts your thread while you are using this function and the new thread uses the function.

Re: Function Parameters/Return Values

Posted: 11 Oct 2018, 20:09
by User
Is that even a problem? The function performs very quick operations, I don't see that as a problem!

Anyway, "Critical" is always an option, or, if ones really don't care about using all the time "temp vars" to store "temp values", well, "returning objects" is always an option too!