I'm finally seeing how subroutines can improve some macros I have so I decided to sit and do some tests. I gotta be honest: it was a bit confusing (unfortunately the Wiki pages don't help a lot of the times without real examples and images...), but here's what I got so far (I will be as clear as possible, and include images, in case other people in the future struggle with the same thing). If you see something wrong with my post, please feel free to share it.
How can subroutines be used and some of their benefits?
For those new to subroutines such as myself, what subroutines can help with is to create a "template" that can be used by 1 or more macros (I would say that more macros
is when it actually makes more sense, even though I know it's not limited to that scenario, of course), similar to how we use variables to avoid editing multiple things when we can just update a single variable value and everything else that depends on that variable is now updated.
So in the case of subroutines, here's an example:
I have the calculation a+b=c
I can set that as a subroutine, so all I'm creating is a "template" that uses that formula.
Now I can have several macros that use that same calculation, but where I can just set different values to a
and b
on each macro. For example:
Macro 1
variable a = 2
variable b = 3
Macro 2
variable a = 34
variable b = 20
Macro 1 and Macro 2 both run the same subroutine (e.g. SUB Calculation
) and perform the same calculation a+b=c, but each macro returns a different c
, which then can be saved as a variable Local__calcSum
, for example.
How to create and use a subroutine?
-
Create the subroutine (e.g.
SUB Calculation
) and set the trigger toSubroutine Trigger
when clicking the green + button.
-
Define what comes from the main macro (e.g.
Main Macro
) to be processed in the subroutine. For example, this subroutine will perform a simple calculation and uses 3 variables that use values coming from myMain Macro
:
-
Add the calculation itself using those variables:
-
Then use the
Return Result
action to send the result from the calculation back toMain Macro
-
The full subroutine
SUB Calculation
will look like this:
That's done and whatever value I get from that calculation is now automatically being sent to Main Macro
Now let's create the Main Macro
:
- Add the
Execute Subroutine
action, pick the subroutine (the list will only be populated with macros that are set to be triggered by "Subroutine Trigger" - the first step I mention at the top)
The value that comes from the subroutine, in the Return Result
action, will be sent to the Save result to
field (le'ts say the result of that calculation is 26
, because of the values I set to 2
, 20
, and 4
) and it will be saved to the variable Local__calc
.
- Do whatever needs to be done with that value, in this case I'm just displaying it in a window:
And when I run Main Macro
I get this:
So the full Main Macro
is this:
NOTE: Subroutines don't always need to return a result. They can just receive values/info/parameters from the main macros and just process that, without the main macros depending on a result
. I just used it that way in this post, because it seems easier to understand how they can both interact with each other.
I hope this is all correct and if so, I hope it helps others who struggle with the same thing as I did.
If you see something that's not correct, please let me know so I can learn more about it and also update this topic with accurate information.