Instance Variables as Subroutine parameters

One for the Ver 10 Wiki updates:

The variables section at:

manual:Variables [Keyboard Maestro Wiki]

includes a table cell on contexts in which Instance variable values are accessible:

It seems, empirically, that we could now add a further context to this list – parameter variables for subroutines

So that if I create a macro with a trigger like this, using an instance-prefixed variable name:

I can then call the macro as a subroutine, and will be offered a corresponding (abbreviated) prompt for the parameter value:

Subroutine test Macros.kmmacros (12.5 KB)

1 Like

Because the contents of the parameter variable is passed to the subroutine, you can also use Local variables in the calling routine (as well as a local variable to receive it in the called subroutine). The Local part of the variable is removed from the parameter name, like Instance is here.

As I move many of my Execute Script actions to subroutines I've been switching them to Local variables.

Mostly I'm doing this because a programming habit I have is try to use as small a scope as possible in a subroutine and avoid side effects.

About the only time I use instance variables in subroutines is if I want to return multiple values and don't want to combine them into a single array string to return (or break them back up on the return) but this has been rare so far. Also if I'm modifying a dictionary which are always global anyway.

1 Like