Function Parameters/Return Values
Posted: 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
First, make a function. Give it a name!... No not that name... Not that either...
Something like... MyFunction
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.
Looks kinda... long. Let's shorten than shall we?
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:
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:
This allows the user to get X and Y separately by doing this:
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).
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
First, make a function. Give it a name!... No not that name... Not that either...
Something like... MyFunction
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
}
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
}
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]})
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}
}
Code: Select all
Data := MouseGetPos()
MsgBox, % Data.X " " Data.Y " " Data.Win " " Data.Ctrl
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).