
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
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.