MATLAB - Return Statement



A "return statement" in MATLAB is used to exit a function and return a value or set of values to the caller of that function.

Purpose of the Return Statement

The primary purpose of the return statement in MATLAB is to −

Exit the Function

When the return statement is encountered, the function execution is terminated, and control is passed back to the caller.

Return Values

You can use the return statement to send one or more values back to the caller. These values can be computed within the function and can be used by the calling code.

Syntax of the Return Statement

function [output1, output2, ...] = functionName(input1, input2, ...)
   % Function body
   % Use the return statement to return values
   return;
end

The detailed explanation of the syntax is as follows −

function keyword − This keyword is used to declare a function in MATLAB.

[output1, output2, ...] − This is an optional part of the function declaration, specifying the output arguments that the function will return. You can have multiple output arguments separated by commas.

functionName − This is the name of the function you're defining.

(input1, input2, ...) − These are the input arguments that the function takes. You can have multiple input arguments separated by commas.

% Function body − This is where you define the actual code of the function.

return; − The return statement is used to exit the function and return control to the calling code. It can be followed by the output values that you want to return.

Example of Rreturn Statement in MATLAB

Let's look at some examples to better understand how the return statement works −

Example 1

In this example, the add function takes two input arguments x and y, adds them, and returns the result using the return statement.

function result = add(x, y)   
   result = x + y;
   return;
end

When you execute the same in matlab command window the output is −

>> sum = add(5, 3)

sum =

     8

>> 

Example 2

In this example, the computeSumAndDifference function returns two values (sum and difference) to the caller.

function [sum, difference] = computeSumAndDifference(a, b)    
   sum = a + b;
   difference = a - b;
   return;
end

When you execute the same in matlab command window the output is −

>> [x, y] = computeSumAndDifference(10, 5)

x =

    15


y =

     5

>>

Example 3

In this example, the divide function checks for division by zero and exits the function early if it occurs, preventing any division by zero errors.

function result = divide(x, y)
   % Function to divide two numbers
   if y == 0
      disp('Division by zero is not allowed.');
      return; % Exit the function here
   end
   result = x / y;
end

Let us make a call to the function as shown below −

quotient = divide(8, 2)
quotient = divide(6, 0)

On execution inside matlab command window the output is −

>> quotient = divide(8, 2)
quotient = divide(6, 0)

quotient =

     4

Division by zero is not allowed.
Advertisements