🔙🎓 Call & Return [HS]

by Hub Space

Version 8 (February 23, 2023)

Download (1,961 downloads)

Have you ever wanted to start a flow and have it return some variable or data back without loosing the context of the flow you are currently in?
This is a tutorial to do it as best as i know and establish a payload structure for consistent use of calling a flow and returning data. The amazing power of the subroutine block allows you to use this function call wherever you want in your flow without loosing its context.

Included is an example of calling a flow (function) for multiplication of two numbers. With the multiplication subflow receiving a payload (parameter / arguments) and have it return the product back to the main fiber.

Solved by a callback function and a subroutine, which can be reused for all calls in your flow.

How to use:
Start the 00 Main Fiber and see C&R perform multiplication by calling another flow with two arguments.

Or

Start 01 Main and select a C&R flow (your function/method to call), i.e., 50 Subflow (*C&R).
The calculation n*5 with the parameters 5 was made and written to your log file.


Normally, one would want to use the Subflow (the flow that will be called and returns a value) from a different flow outside. So you can have function calls with return over multiple flows.


If you want to use this in your flow copy blocks 52, 76, 75, 72, 4, 6 and 3, 59, 7. It might be necessary to update blocks 72 and 7 after copying.


NEW:
Version 2 - Index optimization. The Callback fiber is defined in code and doesn't need to be selected.
Version 3 - Validate existing parameter for calling
Version 7 - Refactoring, added comments, add subroutine to use the same callback and variable give/take structure for all function calls.
Version 8 - description, refactoring

Notice:
Do not change the syntax of payload (position of fiber uris is fixed).

I recommend adding (*C&R) to the flow beginning block title in order to indicate that a flow can return a value following the standard presented in this flow tutorial.

How does it work?
A flow can be called by a Flow Start block from anywhere. A payload can be specified to give the flow access to variables in order to manipulate its behavior.
With C&R, we introduce a structure of that payload to tell the flow how it can return data to its caller.
This includes the callback flow and the calling fiber uri. To return the data, the flow starts the Callback with the return data and again the caller fiber uri. Finally the callback will give the return data to the fiber. Thus completing the call and return procedure.

- - - -
Have a look on my other flows. Search for '[HS]'.

5.0 average rating from 3 reviews

5 stars
3
4 stars
0
3 stars
0
2 stars
0
1 star
0
Reports
0

Rate and review within the app in the Community section.