Feature Request: Multiple Subroutine Return Values

Hi, @Zabobon. Thanks for weighing in.

You're right, but the actions at the end of the subroutine and beginning of the caller would remain unchanged, notwithstanding the comment. (I placed these template actions in groups.)

However...

Well my thought was to come up with a method that would function properly if a subroutine was being concurrently used by multiple callers. But now that I think about it, since dictionaries are global, the approach I proposed above would fail. Here's a revision that would work assuming the callers specify unique dictionary names:


DOWNLOAD Macro Files:
Subroutine Examples Macros.kmmacros (9.2 KB)
Note: These macros were uploaded in a DISABLED state. They must be ENABLED before they can be run. If they do not trigger, the macro group might also need to be ENABLED.


Image: caller—Example v2


Image: sub—ExampleReturningValuesUsingADictionary


Yes, and if a caller only calls a subroutine once, then this approach is undoubtedly the simplest way to go. (I think @peternlewis made the same suggestion.) And even in cases when a caller makes multiple calls to a subroutine, values can be segregated if the instance variables are cleared prior to calls 2 to n:



DOWNLOAD Macro Files:
Subroutine Examples Macros.kmmacros (13 KB)
Note: These macros were uploaded in a DISABLED state. They must be ENABLED before they can be run. If they do not trigger, the macro group might also need to be ENABLED.


Image: caller—Example v3


Image: sub—ExampleReturningValuesUsingInstanceVariables

1 Like

In fact you don't even have to clear the instance variables as that will happen each time the Subroutine is run and replaces the values with new values. So, in my quick testing you don't need these Actions:

Click to show image

Sorry, I should have been more clear.

If the subroutine is trivial like with my example, it wouldn’t be necessary to clear the instance variables. But suppose it’s more complex and it includes conditional logic that caused cases where the setting of an instance variable is skipped. In these cases, there would be no way to fully inform the caller unless the caller effectively initializes the variable before calls 2 to n. (Since variables values are null before use, the manual clearing is unnecessary for call 1.)

On a related note, with the approach I used above in v2, the clearing is done when the dictionary is set to JSON {}.

After rereading this thread, experimenting with various options, and considering @Zabobon's comments, I think the below is the approach I will use to return multiple values from a subroutine. As I see it, here are the advantages:

  • Syntax for subroutine and caller is relatively simple.
  • Instance variables are not required.
  • Subroutine can be concurrently called by multiple callers with no risk of cross-contaminating values.
  • Even in cases when the caller makes multiple calls to the subroutine, it's easy to differentiate the returned values in the caller.

Comments still welcome. :grinning:



DOWNLOAD Macro Files:
Subroutine Examples Macros.kmmacros (14 KB)
Note: These macros were uploaded in a DISABLED state. They must be ENABLED before they can be run. If they do not trigger, the macro group might also need to be ENABLED.


Image: caller—Example v4


Image: sub—ExampleReturningValuesUsingJSON