# All Types of Matlab Function Creation With Examples

Matlab creation function plays an essential role in implementing where the Matlan file name and the function name should be the same. There are different kinds of function creations in Matlab that are used for different purposes. Programmers might get confused because of the availability of these functions, but this blog will help you to understand each function with its syntax and example so that one can use them as per their preferences. Before proceeding to the Matlab function creation, get some details of what are the functions in Matlab.

### What is the function in Matlab?

The function in Matlab is the group of the statements which are used to perform a specific task in Matlab, and these functions are defined in a separate file. The file name and the function always have the same name. The functions that are used for the variables in their workspace are known as a local workspace, and another separate variable can be accessed through the command prompt, and these are known as the base workspace. The functions can take more than a single input argument and return the more than a single output argument.

### Syntax of Matlab function creation

function [out1,out2, …, outN] = myfun(in1,in2,in3, …, inN)

### Example of Matlab function creation

The example given has a function as mymax, and the file name mymax.m. it can take four numbers as the arguments and return the maximum value of the input.

Then the coding for this function can be written as:

function max = mymax(a1, a2, a3, a4)

%This function measures the maximum of the

% four numbers that are given as input

max =  a1;

if(a2 > max)

max = a2;

end

if(a3 > max)

max = a3;

end

if(a4 > max)

max = a4;

end

The initial function’s line has the keyword function and has the function name and argument order.

You can use the input as:

mymax(54, 87, 90, 22)

The Matlab will give the result after executing the program as:

ans= 90

### Anonymous Function

This Matlab function creation is just like an inline function used for traditional coding languages, which is defined in the single statement line of Matlab. It has one expression, and it could have any of the numbers as input or output arguments.

The anonymous function can be defined at the right of a command line or within the script or the function. This is how you can use Matlab function creation without generating the file for them.

#### The syntax for the anonymous function

f = @(arglist)expression

#### The example for the anonymous function

The given example has anonymous function with name power, that has two input and it returns the initial number with raised to the power of the next number. The code for this script file will be as:

power = @(a, n) a.^n;

Output1 = power(5, 2)

Output2 = power(64, 0.5)

Output3 = power(10, -10)

Output4 = power (2.5, 2.5)

This program will execute this, and output as:

Output1 =  25

Output2 =  8

Output3 =  1.0000e-10

Output4 =  9.8821

### Primary and Sub-Functions

Functions that are separate from an anonymous function can be defined in a separate file. Each of these function files includes the primary function, which is shown initially, and the number of optional sub-functions which are come after the prime functions and used as per the requirement.

These prime functions can be known from the file’s outside, which defines them, whether from the command lines or from any of other functions, whereas sub-function can call from outside the function file and other function or command line. Sub-functions can appear only in another sub-functions and primary function within a function file which defines them.

#### Example of primary and sub-function

Write a function name as quadratic, which can be used to measure the roots of the quadratic equation. This function can have three inputs, the linear coefficient, the quadratic coefficient, and the constant term, and then it will return the value of roots. The name of function file as quadratic.m will have the primary function as quadratic and the sub-function as disc, which used to measure the discriminant.

Programming with quadratic.m as function file, then it can be as:

%this function gives the roots of

% It has 3 input arguments

% which are the co-efficients of a2, x and the

%constant term

% It returns the roots

d = disc(x,y,z);

a1 = (-y + d) / (2*x);

a2 = (-y – d) / (2*x);

end   % end of quadratic program

function dis = disc(x,y,z)

%function measures the discriminant

dis = sqrt(y^2 – 4*x*z);

end   % end of sub-function

You can use this programming, and input will be given as:

Then, the output will be shown as:

ans= 0.7321

### Nested Matlab function creation

This function can be used within the main body of other functions, and these can be called as nested functions. The nested functions have some or all the elements of the other functions. These Matlab function creation can be defined with the scope of other functions, and they can be accessed with the containing workspace of function.

#### Syntax of the nested function

function x = A(p1, p2)

B(p2)

function y = B(p3)

…

end

end

#### Example of the nested function

Let’s take the above-mentioned example of quadratic equation, in this example disc function will be taken as the nested function.

This has a function file named as quadratic2.m  and program will be as:

function disc  % nested function

d = sqrt(y^2 – 4*x*z);

end   % end of function disc

disc;

x1 = (-y + d) / (2*x);

x2 = (-y – d) / (2*x);

end   % end of function quadratic2

This will give the output for the input value of :

MATLAB will execute the above code and return the output as−

ans =  0.73205

### Private functions

These Matlab function creation are the primary functions that are only visible to the limited group of the functions. If the programmer does not want to run these functions, then they can generate these functions as private functions. These functions remain in subfolders that have a particular private name. These are visible to the parent functions folder.

#### Example of private function

Again take the example of the quadratic equation. This time the disc function for measuring the discriminant, then the private function will be considered. Write a subfolder with the working directory and store this function as:

function dis = disc(x,y,z)

%function calculates the discriminant

dis = sqrt(y^2 – 4*x*z);

end      % end of sub-function

Write a function as quadratic3.m in the working directory and then write the code as:

%this function will return the roots of

% It takes 3 input arguments

% that is the co-efficient of a2, a and the

%constant term

% It will return the roots

d = disc(x,y,z);

x1 = (-y + d) / (2*x);

x2 = (-y – d) / (2*x);

Run the program with the input value as:

MATLAB will implement the above program and return the output as−

ans =  0.73205

### Global variables

It can share with more than one function. But to use it, one requires to declare the function as global for all the functions. When the programmer wants to use that variable from its base workspace, declare it within the command line. The global declaration should carry out before the actual use of the function. It can be good to use the global variable’s name in capital letters to differentiate them for other variables.

#### Example of global variables

For this, make a function file with name average.m and write this program as:

function avg = average(nums)

global TOTAL

avg = sum(nums)/TOTAL;

end

Write a script file and write the following program in it −

global TOTAL;

TOTAL = 5;

n = [34, 25, 33, 40, 38];

av = average(n)

When one execute the program, it will show the following output−

av =  34

### Conclusion

The Matlab function creation is used to perform the specific task with the help of using the different types of functions. There are 5 types of functions that can be used for different purposes, and these functions are anonymous, primary and sub-function, nested, private, and global variable functions. All of these have different uses and the syntax method, and the programmer can use them as per the requirement of the coding. This blog has provided all the necessary details on these functions, that help you to understand the syntax and example of these Matlab functions.

If you have issues related to the Matlab assignments and homework, then you can take our Matlab Assignment Help, which is available at minimal prices. We provide you well-formatted and well-syntax data in your assignments that are plagiarism-free too. Whenever you are facing any programming issue, just contact our customer support executives who are available 24/7 and solve your coding difficulty in the best possible ways