Function Parameters/Return Values

Helpful script writing tricks and HowTo's
User avatar
Delta Pythagorean
Posts: 416
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Equestria

Function Parameters/Return Values

23 Oct 2017, 09:20

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] [Download] GeSHi © Codebox Plus

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] [Expand] [Download] GeSHi © Codebox Plus


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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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).
User avatar
derz00
Posts: 495
Joined: 02 Feb 2016, 17:54
GitHub: derz00
Location: Middle of the round cube

Re: Function Parameters/Return Values

23 Oct 2017, 10:26

Hey you missed some %'s. It should be

Code: [Select all] [Download] GeSHi © Codebox Plus

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:
try it and see
...
andreysOdomo

Function Parameters/Return Values

13 May 2018, 21:43

is the return value of a function always the last data type before the function ends with ";;"
User avatar
gregster
Posts: 1346
Joined: 30 Sep 2013, 06:48

Re: Function Parameters/Return Values

13 May 2018, 22:27

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.

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 4 guests