Categories

# Calling Functions in Matlab

Calling a function, also referred to as invoking a function, is used to pass the control of the program to the required function, which in turn performs defined tasks and returns the control of the program back to the main program if the return statement of this function is executed or if the function-ending brace is encountered. A function is called by simply passing the required arguments or parameters into the name of the function. In this topic, we are going to learn about Calling Functions in Matlab.

## Examples of Calling Functions in Matlab

Let us now understand the syntax to call the above functions in MATLAB:

## Example 1:

In this example, we will learn how to call the ‘remainder function’ in MATLAB.

## Syntax:

``A = rem(x, y)``

## Below are the steps to be followed:

1. Initialize ‘Dividend’ and ‘Divisor’
2. Pass the ‘Dividend’ and ‘Divisor’ as parameters to the rem function

## Code:

``x = 29[Initializing the Dividend]``
``y = 5[Initializing the Divisor]``
``A = rem(x, y)``

[Passing the above variables to the rem function] [We are calling or invoking the function ‘rem’ bypassing the variables declared as input parameters to it]

## Input:

``````x = 29
y = 5
A = rem(x, y)``````

## Output:

As we can see in the output, we have obtained the remainder of the input variables by calling the rem function.

## Example 2:

In this example, we will learn how to call ‘integral function’ in MATLAB.

## Syntax:

``A = integral(fx, min, max)``

## Below are the steps to be followed:

1. Create the function to be integrated. for this example, we will take fx = x^4 * exp(-x) * cos(x) as our input function
2. Pass the function along with the limits of integration as the parameters to the integral function

## Code:

``fx = @(x)x.^4.*exp(-x).*cos(x);``

[Creating the function ‘fx’ in MATLAB]

``A = integral(fx, 0, Inf)``

[Passing the above function to the integral function. Here we have also passed the limits of integrations] [We are calling or invoking ‘integral’ bypassing the function to be integrated and limits of integration as parameters]

## Input:

``````fx = @(x)x.^4.*exp(-x).*cos(x);
A = integral(fx, 0, Inf)``````

## Output:

As we can see in the output, we have obtained the integral of the input function by calling the integral function.

## Example 3:

In this example, we will learn how to call the ‘max function’ in MATLAB.

## Syntax:

``A = max(Z)``

## Below are the steps to be followed:

1. Initialize the array whose maximum value we need
2. Pass the array as a parameter to the max function

## Code:

``Z = [2 5 8 1 10];[Initializing the array]``

`A = max(Z)`[Passing the array to the max function] [We are calling or invoking ‘max’ bypassing the array whose maximum value we need as a parameter]

## Input:

``````Z = [2 5 8 1 10];
A = max(Z)``````

## Output:

As we can see in the output, we have obtained the maximum number out of all the numbers present in the array by calling the max function.

## Example 4:

In this example, we will learn how to call ‘mean function’ in MATLAB.

## Syntax:

``A = mean(Z)``

## Below are the steps to be followed:

1. Initialize the array whose mean value we need
2. Pass the array as a parameter to the max function

## Code:

``Z = [2 0 8 11 10];[Initializing the array]``

`A = mean(Z)`[Passing the array to the mean function] [We are calling or invoking ‘mean’ bypassing the array whose mean value we need as a parameter]

## Input:

``````Z = [2 0 8 11 10];
A = mean(Z)``````

## Output:

As we can see in the output, we have obtained the mean of the numbers present in the array by calling the mean function.