Expanding functionality: New return function

Reading time: 4 minutes

Developing applications in Linx follows common programming paradigms. This means that it will use variables, loops and if statements in a similar fashion to a traditional programming language. With this in mind, a recent update (6.4.1) introduced the Return function. The Return function is the new standard for returning values to the result of a function or to exit a function at any point. This post will go over what the Return function does and how it can be used.

 

Why was it added?

A challenge in previous versions of Linx used to be that there was no way to exit out of a function quickly. If you had complex nested functions, if-else statements or loops, it could become quite troublesome as the operation had to complete to the end before the function would end. 

As an example, you have a complex function that performs many operations. Previously the function would have to execute all the way through. With the Return function, you can instruct the function to stop gracefully at a specific point and return a value if required. Another example is a complex loop where defining a stop or exit criteria was difficult. With the Return function, you can exit the loop at a specific point (if you do not want to execute anything below the loop).

Another advantage of the Return function is that it is more explicit and transparent about what is returned and when. 

Return is almost universally used in programming languages to return a value from a function or method. Because Linx follows common programming conventions, it is only natural that it should have a Return function. It is important to take a quick look at how Return is used in a programming language.

 

Return in Code

In programming, Return is commonly used to terminate the execution of a function or method and to return a value in the output. Control is returned to the function or method that called the ‘child’ function along with the resulting value from that function. 

For example, a method in C# that calculates if a passed-in number is a prime number may look like this:

[et_pb_dmb_code_snippet code=”cHVibGljIHN0YXRpYyBzdHJpbmcgaXNQcmltZShpbnQgeCkKewogICAgaW50IGksIG0sIGZsYWc9MDsgCiAgICAgbT0oeC8yKTsgICAgCiAgICAgICAgIGZvcihpID0gMjsgaSA8PSBtOyBpKyspICAgIAogICAgICAgICAgeyAgICAKICAgICAgICAgICBpZigoeCAlIGkpID09IDApICAgIAogICAgICAgICAgICB7ICAgIAogICAgICAgICAgICAgcmV0dXJuICgiTnVtYmVyIGlzIG5vdCBQcmltZS4iKTsKICAgICAgICAgICAgfSAgICAKICAgICAgICAgIH0gICAKICAgIHJldHVybiAoIk51bWJlciBpcyBQcmltZS4iKTsKfQ==” admin_label=”Code Snippet” copy_button=”on” _builder_version=”4.14.8″ _module_preset=”default” global_colors_info=”{}”][/et_pb_dmb_code_snippet]

The method receives an integer, loops to check if it is divisible by anything other than itself and returns if it is a prime number or not. Note that in the if statement a return statement is used to immediately return that the number is not a prime number when it is divisible by any other number other than itself or 1. Here the loop does not need to finish in order for the function to determine a result, saving time and resources. 

 

Using Return in Linx

Using the Return function is as simple as dragging it from the plugins tab onto your process. It is important to note that the return value of a function can no longer be set by using a SetValue component. This is now exclusively done by using the Return function. 

Taking the same example as above, you can calculate if a number is a prime number in Linx in exactly the same way:

Linx process that calculates Prime numbers using the Return function

The prime number is calculated by checking if the number is divisible by any number smaller than half of that number. Half of the number is calculated as the DivisionLimit, and the loop then divides the number by every number from 2 up to the DivisionLimit using modulo and checking that the result is 0. If the result is 0, the number is divisible by another number and is thus not a prime number.

As mentioned the Return function is now exclusively used to return a value from a function.  Upon opening older solutions in Linx 6.4.0, most of the functions they contain will be automatically upgraded. This upgrade will result in some of the components being converted. However, the upgrade will not affect the logic of your solution and does not require any intervention from you. Please do however go over your solution to ensure that it is still correct because if you used the SetValue function to set a result prematurely, this will be transformed into a Return function, meaning that the function will exit at that point. It is recommended that a backup be made of your solution before upgrading.

With all of the above said, the SetValue function is still used to set parameters and other internal values used in the function. 

 

Sign up to our Newsletter