Matlab

Return Function in MATLAB

When we call functions to process data in MATLAB, the dynamics of that process often depend on several factors, such as the type of data sent and the results of threads within the same function. These conditional factors can cause the function to return to its calling point before it is fully executed. To avoid the execution of the necessary code, data processes incompatible with the function, or simply because the final result was obtained, MATLAB offers the return() function for this purpose.

The following article explains in detail the use of the return function in MATLAB.

This function is part of most programming languages of all time, and its syntax and operation are the same for all of them. In the following, we will show you how to use this function in MATLAB with practical examples and pictures.

Syntax of the Return Function in MATLAB

return;

Description of MATLAB Return Function

Return ends the currently executing function and returns the program flow to the code from which the function was called. When a function is called in MATLAB, the program flow jumps to it, executes it completely, and returns to continue on the next line of the call. However, sometimes it is necessary to end the function somewhere instead of executing it thoroughly. For this reason, MATLAB, like most programming languages, provides the return function for this purpose. These returns bring significant benefits to the program, such as speed, by avoiding the execution of unnecessary code fragments. They also provide stability in the program’s execution since a function can be terminated according to the conditions established by the user programmer to call the return. For example, if the input data type is incompatible with the function, no one will execute and return to the next line of the program that called it.

These returns are usually placed inside the function in “if” conditionals, “switch”, or other types of jump conditions where a variable, result, data type, etc., determines whether the execution of the function should continue or whether it should terminate and return to the function from which it was called. Next, we will see some practical examples of creating functions with returns to help you better understand the use of this resource.

How To abort the Execution of a Function With “return” in MATLAB If the Input Parameters Are out of Range

In this example, we will create a simple console application to show you how to return; it works in MATLAB. This application consists of two parts; the first is the application where we enter the values through the console, see the results and call the function “return_example()”. Here is the code for this section:

while 1
  prompt = 'Enter value and press enter.  Ctrl+c to exit';
  x=input(prompt);        % Input value
  clc();                  % Clear console
  return_example(x);      % Function return_example() call
end

The second part of this code is the function “return_example()”. This function consists of an “if” conditional with short-circuit logic that analyzes the parameters of “x”. If they are out of range from 0 to 100, it displays the following message on the screen: “x” out of range, return” and returns to the next line of the call to the function “return_example”, interrupting the full execution of the function.

Create a script, paste the complete code, and press “Run”. Enter various values using the command console and press Enter. You can also set breakpoints on lines 13 and 17 to stop execution at this point and control the program flow.

function result = return_example(x)

       if ( x  100)      % x is out of range from 0 to 100
          disp ‘ "x" out of range, returns to the next line  "return_example" call’
          return;                      % Return terminates the return_example()                
       end                             % and return to the next line of the call                        
       
       % If the value of "x" is within the specified range, the function
       % return_example() continues here.  

       disp ‘"x" in range, function executed completely’


end

If the parameters of “x” are within the range specified in the “if” conditional, the function return_example() will be fully executed. Otherwise, its execution will be interrupted and return to the code from which it was called.

The following image shows what happens when you enter a value within the specified range. In this case, the return_example function is executed completely. As you can see, the program flow is stopped at the breakpoint of line 17.

The following image shows what happens if you enter a value that is outside the specified range. In this case, the return_example function is interrupted by “return”, and the program flow returns to the next line of the return_example() call. As you can see, the program flow is stopped at the breakpoint in line 13.

How To Abort the Execution of a Function With “return” in MATLAB If the Input Data Types Are Not Compatible

In the previous example, we showed you how to stop the execution of a function and return the program flow to the code that called it. This is accomplished by placing as a condition for it a predetermined range of values in the conditional “if” that we placed inside the code of the return_example() function.

In the following example, we will also create a console application and use the return function to abort the return_example2 function if the input data type sent in the call is incompatible with those accepted by the function.

As in the previous example, we will create a console application that consists of two blocks. The first block is for entering data, displaying results, and calling the return_example2() function; the second block is for this function. Now, let us look at the complete code for the following example:

while 1
  prompt = 'Enter value and press enter.  Ctrl+c to exit';
  x=input(prompt, “s”);   % Input value
  clc();                  % Clear console
  return_example2(x);     % Function return_example() call
end


function return_example2(x)

       if  isnan(str2double(x))        % if x is no numeric
          disp ‘The data type is not supported, returns to the next line call’
          return;                      % Return terminates the return_example2()                
       end                             % and return to the next line of the call                        
       
       % If the value of "x" is within the specified data types, the function
       % return_example() continues here.  

       disp ‘The entered value is numeric, function executed completely’


end

Unlike the previous example, the input data type entered from the command console is a string, which is converted to a numeric value using str2double() within the return_example2() function. If the result is a non-numeric value, the function is terminated with “return”. If, on the other hand, the entered value is numeric, the return_example2() function continues with its execution.

The following image shows the result in the command console window when a numeric value is entered:

The following image shows the result in the command console window when a non-numeric or incompatible value is entered with the return_example() function:

Conclusion

In this article, we have explained how this simple but very useful function works in MATLAB. For better understanding, we have included two practical examples in which we create simple console applications that use “return” to determine the abort or full execution of a function based on several conditions. We have also included images that show how these examples work in the MATLAB environment. We hope you found this MATLAB article helpful. Check other Linux Hint articles for more tips and information.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).