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 −

matlab function

The execution in matlab is as follows −

>> number = 4;
>> [square,cube] = calculateSquareAndCube(number)
square =

   16

cube =

   64
Advertisements