This Linux Hint article explains function calls, their structure, and input and output arguments in MATLAB.

Next, we will look at some practical examples with different functions. We will add fragments and code images showing the different modes of function calling in the MATLAB environment.

## What is a Function in MATLAB?

A function is a fragment of code. When a function is called, the program flow jumps into that code, executes it by performing a specific task or data processing, and then returns with a result to the following code line from which the function was called. These codes can be part of MATLAB’s extensive library, but we can also create our function libraries.

A function is divided into Name, Input Arguments, and Output Arguments. The following is the structure or syntax of a function in MATLAB:

Below, we describe what each of these parts making up the structure of a function is about:

**Name: **The name of the function and the one used to call it.

**(input arguments): **This is the data sent when the function is called. This data can be scalar matrices, vectors, 2D, and multidimensional or vector cell matrices. They can also control flags of the function’s various options for processing input or output data.

The input arguments should always be enclosed in parentheses, as shown below:

In cases where there is more than one input argument, they must be separated by commas and specified in the order given by the syntax of the respective function.

**Output arguments:** These are the results that the function returns when it returns from the call. These output arguments can be in the form of scalars, vectors, 2D, and multidimensional arrays. In cases where the output arguments are more than one, they must be enclosed in square brackets and separated by commas, as shown in the following syntax:

## Calling Functions From the MATLAB Command Console

The command console of the MATLAB environment allows us to call functions from there. So, it’s an excellent resource for learning how to use and call functions because we can run code from there and see the results in real-time. The following image shows this tool executing functions and displaying their output in MATLAB:

## How To Call a Function in MATLAB

To call a function, we need to write in a line of code in the following order, output argument, followed by the “=” character, function name, and then the input arguments in parentheses.

When we want to use a function, we need to be clear about what it does, what data we want to process, and where we want to store the results. Below, we see some examples of calling different functions.

## How To Call Functions, Process Data, and View Results in MATLAB

The most commonly used functions in MATLAB are those sent with data and return a result. In this example, we call the function “sqrt()”, which returns the square root of “x” in “a”, which in this case is a scalar that we created earlier and assign an arbitrary value to. Below, you can see the code snippet for this:

a = sqrt ( x )

Copy and paste this snippet into a script or command line. When you run it, create the scalar “x” and call the sqrt() function with “x” as input and “a” as output. The result can be seen in the MATLAB console.

In the following excerpt, the input “x” for sqrt() is the result of the function “randi()”. This function returns a random value in the number range specified in its input argument, which, in this example, is 100.

a = sqrt ( x )

The result is a random value in “x” and the square root of this value in “a”.

## How To Nest Functions or Operations in Input Arguments With MATLAB

If the input arguments to a function result from an expression or function, you can nest those arguments. The following is the code from the previous example, but with the randi() function nested within the arguments of sqrt():

The function assumes that its input argument results from the nested function. The same is true for any kind of expression.

The following snippet shows that the input argument of sqrt() results from a nested expression:

c = 7;

a = sqrt ( (b + c) )

## Calling Functions From the Expression of a Conditional

Also, in MATLAB, it is possible to call a function from a conditional expression, and its result is the condition. This is as convenient as nesting functions because it saves lines of code and increases readability. Here is an example where we use the condition “if”, and the condition is the result of the function isnumeric():

if isnumeric ( x )

end

## How To Call Functions With Multiple Output Arguments in MATLAB

In MATLAB, some functions return with multiple arguments. These must be enclosed in square brackets before the “=” sign, separated by commas and in the order given by the syntax of the function in question.

In the following example, we call the min() function, which returns a vector with the minimum value of each row of its input array and another vector with each value’s position index. Below, we see the code of this example where we create the array “x” with random values generated by the randi() function nested in min(). The output arguments are “minimum” and “index”. The results can be seen in the command console.

The figure shows that the function min() returns two vectors, minimum and index, with their respective results when called.

## Conclusion

In this article, we have explained everything you need to know to master the technique of MATLAB functions. We have also shown it as a function in MATLAB, its input and output structure, and the syntax for each calling method. In addition, we include practical examples of calls with code fragments and images showing the different methods for function calls in the MATLAB environment. We hope that this MATLAB article was helpful for you. See other Linux Hint articles for more tips and information.