
- 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 - Function Arguments
In MATLAB, a function takes in arguments, which are variables or values passed to it, and uses these arguments to perform specific operations. These arguments are essential for providing input data to the function and allowing it to execute its defined tasks.
It's very important to know how to make use of function arguments in MATLAB programming. In this chapter, we'll explore MATLAB function arguments, covering their types, how to use them, and best practices.
MATLAB Function Arguments
Let us first discuss the best practices to keep in mind for function arguments.
Specify the Types of Arguments
Explain what kind of data and format you expect in your function's comments. This helps users know how to use your function correctly.
Check Argument Validity
Validate input arguments within your function to ensure they meet the expected criteria. This prevents runtime errors and enhances code robustness.
Use Descriptive Names
Choose meaningful names for your input and output arguments. This improves code readability and makes it easier for others (and yourself) to understand the purpose of the arguments.
Minimize Global Variables
Avoid using global variables within functions, as they can make your code less modular and harder to debug. Instead, pass necessary data as function arguments.
Types of Function Arguments
Here various types of function arguments −
1. Input Arguments (Parameters)
These are values or variables that you pass to a function for it to use during its execution. Input arguments allow you to provide data that the function will work on. You can pass various types of data, such as numbers, arrays, or even more complex data structures.
2. Output Arguments (Return Values)
Some functions in MATLAB return results that you may want to capture and use in your code. These are referred to as output arguments. You specify these arguments in the function's definition, and the function stores its results in these variables for you to use.
Syntax for Defining and Using Function Arguments
To define function arguments in MATLAB, you use the function keyword followed by the argument list in parentheses. Here's a basic syntax template −
function outputArg = functionName(inputArg1, inputArg2, ...) % Function body % Use inputArg1, inputArg2, ... to perform calculations outputArg = result; % Assign the result to outputArg end
functionName is the name of your function.
inputArg1, inputArg2, ... are the input arguments (parameters) that you pass to the function.
result is the value or variable that you assign as the output argument (return value) within the function's body.
Passing Arguments to Functions
When calling a function, you pass the required input arguments within the parentheses. The function then uses these values to perform its tasks.
result = functionName(argument1, argument2);
Above we have seen the basic way of how function arguments are passed to do calculation.
Now we will take a look at a more advanced feature in matlab called as arguments.
In MATLAB, the arguments block is a powerful feature that allows you to define and manage function input and output arguments with flexibility. It provides detailed control over the properties of these arguments, including their names, dimensions, data types (classes), and validation rules.
The arguments block is used to specify the function's input and output arguments.
Syntax
arguments argName1 (dimensions) class {validators} = defaultValue ... argNameN end
argName1, ..., argNameN − These are the names of the function's arguments. You list all the arguments here, both input and output.
(dimensions) − You can specify the dimensions or size of the argument. For example, you can define a matrix argument as (2,3) to indicate a 2x3 matrix.
class − This specifies the expected data type or class for the argument.
{validators} − You can include validation functions or conditions inside curly braces to check if the input values meet specific criteria.
= defaultValue − You can provide default values for input arguments, which are used if the argument is not provided when the function is called.
Here is an example using arguments −
function [square, cube] = calculateSquareAndCube(x) % Calculate the square and cube of a number % Define input argument arguments x double end % Calculate square and cube square = x^2; cube = x^3; end
The above function calculates the square and cube of a given number.
Function Declaration − The function is declared using the function keyword, and it accepts one input argument x.
Input Argument Specification − Inside the function, the arguments keyword is used to specify the input argument, x, as a double-precision floating-point number. This means that the function expects x to be a real number with double-precision data type.
Calculation − After specifying the input argument, the function calculates two values −
- square − It calculates the square of the input x by raising it to the power of 2 using the ^ operator.
- cube − It calculates the cube of the input x by raising it to the power of 3 using the ^ operator.
Return Values − The function returns two values, square and cube, as specified in the function signature: [square, cube]. These values are the results of the calculations.
In matlab the function is saved as calculateSquareAndCube.m file as shown below −

The execution in matlab is as follows −
>> number = 4; >> [square,cube] = calculateSquareAndCube(number) square = 16 cube = 64