Categories

# 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

## 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 variableMathematical equation ( function definition )Function variable(input variable value)`

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

## 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

## 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

## Advantages of Matlab Create Function

There are few advantages described below.

• 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 the function name .there is no need to write function definition all the time.
• Because of the 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.