## Introduction to Matlab Create Function

Matlab Create Function play an important role for function implementation is the function name and Matlab file name should be the same. Matlab Function is defined as is a set of instructions that performs specific operations in Matlab, functions need a separate file in Matlab. It is implementation divided into three parts declaration of a function, calling a function and definition of function means function body.

In a function declaration, we declare the name of the function and we declare then what are the parameters are going to operate inside the function body. In function definition we define the function, here we write the actual program logic and statements and the last part is calling a function in this we just call the function whenever required. Matlab Create Function declaration function can assign multiple parameters that can accept multiple values as input and in the definition of a function can return multiple values or multiple arguments. There are various functions in Matlab such as Anonymous functions, Primary and Subfunctions, Nested functions and Private functions. In private function, we can hide important data from unwanted users so private functions provide privacy to code.

### Examples and Syntax

The following examples and syntax are given below:

#### Example #1: Simple function program

Let us consider we wish to solve one mathematical equation, and the equation is

Eq = (x–y)*32

**Syntax:**

`function variable name = function name (parameter list)`

Function definition ( statements )

end

The following table is a program illustration of the above example

Matlab editor |
Command window |
Output |

function [ eq] = myfunction ( x , y ) eq = (x–y)*32 ; end *file stored as myfunction . m |
x = 4 y = 3 eq = myfunction ( x , y ) |
x = 4 y 3 eq = 32 |

#### Example #2: Anonymous function

Let us consider equation x^2 – log x

To solve the above function we need to create an anonymous function and then need to write a function definition. We can write all the statements of code in the command window.

**Syntax :**

`Function handle variable = @ input variable`

Mathematical equation ( function definition )

Function variable(input variable value)

4.5 (2,351 ratings)

View Course

The following table illustrates the Matlab code and output of example 2 by using an anonymous function.

Matlab command window |
Output |

y=@x)x . ^ 2–l g( x ) x ( 1 : 1 0 ) p = y ( x ) |
y = @ ( x ) x . ^2–l g (x) p = 14.6137 |

#### Example #3: Sub-functions

If there is more than one function in the program then the second function is called sub-function or primary function.

**Syntax:**

`Function = first function name`

Statements ( function body)

End

Function = second function name

Statements

End

Following table illustrate the Matlab code and output of example 3 by using primary function

Matlab Editor window |
Command window |
Output |

function [ xval 1 , xval2 ] = quadratic ( a1 , b1 , c1 ) op = disc ( a1 ,b1 ,c1 ) ; xval1 = ( -b1 + op ) / ( 2 * a1 ) ; xval2 = (-b1 – op) / ( 2 * a1 ) ; end function dis = disc ( a1 , b1 , c1 ) dis = sqrt(b1^2– 4 * a1 * c1 ) ; end file stored as quadratic.m |
Quadratic ( 10 , 43 , – 54 ) | ans =1.0158 |

#### Example #4: Nested Functions

If the occurrence of any function is inside another function then it is called a nested function.

Let us consider one example. There are two functions quadratic2 and quadratic1, here qudratic1 function is inside the quadratic2 function, therefore, it is called a nested function.

**Syntax:**

`Function = main function name ( parameter list )`

Function = nested function name ( parameter list )

Statements ( function definition )

End

End

Following table illustrate the Matlab code and output of example 4 by using nested function

Matlab Editor window |
Command window |
Output |

function [ xval1 , xval2 ] = quadratic2 ( r1 , r2 ,r3 ) function quadratic1 op = sqrt ( r2 ^ 2 – 4 * r1 *r3 ) ; end quadratic1 ; xval1 = ( – r2 + op ) / ( 2 * r1 ) ; xval2 = ( – r2 – op) / (2 * r1 ) ; end *file stored as quadratic2.m |
>> quadratic2 ( 45 , 52 , 38 ) | an s = – 0.5778 + 0.7146 i |

### Advantages of Matlab Create Function

- The main advantage of functions is, it keeps the program in an organized way and avoids unnecessary repetitions.
- It breaks long codes into smaller blocks.
- We can reuse code again and again just by calling function name .there is no need to write function definition all the time.
- Because of reusability property, it saves space that is memory and executes code easily.
- We can easily manage the flow of control by using functions in programming.
- Functions increase the readability of programming.
- Modifications become easy if functions are present in programs.
- It reduces the occurrence of errors.
- It reduces the complexity of programs.

### Conclusion

Functions have main property reusability because this complex code becomes easy so that programming becomes efficient if we use functions. It is very simple to declare and define any function Matlab. It increases the readability of the program as well as improves the understanding of the program.

### Recommended Articles

This is a guide to Matlab Create Function. Here we discuss the introduction, different examples, and syntax along with the advantages. You can also go through our other suggested articles to learn more–