Function Parameters/Return Values

Helpful script writing tricks and HowTo's
User avatar
Delta Pythagorean
Posts: 385
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.
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.

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
Posts: 404
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

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 5 guests