How to write a function in MATLAB

How to write a function in MATLAB

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 = (xy)*32


function variable name = function name (parameter list)
Function definition ( statements )

The following table is a program illustration of the above example

Matlab editorCommand windowOutputfunction [ eq] = myfunction ( x , y )
eq = (xy)*32 ;
*file stored as myfunction . mx = 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)

Popular Course in this category
How to write a function in MATLAB
MATLAB Training (3 Courses, 1 Project)3 Online Courses | 1 Hands-on Project | 8+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (7,690 ratings)
Course Price

View Course

Related Courses
R Programming Training (12 Courses, 20+ Projects)All in One Data Science Bundle (360+ Courses, 50+ projects)

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

Matlab command windowOutputy=@x)x . ^ 2l g( x )
x ( 1 : 1 0 )
p = y ( x )y =
@ ( x ) x . ^2l g (x)
p =

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.


Function = first function name
Statements ( function body)
Function = second function name

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

Matlab Editor windowCommand windowOutputfunction [ xval 1 , xval2 ] = quadratic ( a1 , b1 , c1 )
op = disc ( a1 ,b1 ,c1 ) ;
xval1 = ( -b1 + op ) / ( 2 * a1 ) ;
xval2 = (-b1 op) / ( 2 * a1 ) ;
function dis = disc ( a1 , b1 , c1 )
dis = sqrt(b1^2 4 * a1 * c1 ) ;
file stored as quadratic.mQuadratic ( 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.


Function = main function name ( parameter list )
Function = nested function name ( parameter list )
Statements ( function definition )

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

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

Advantages ofMatlab 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.


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.

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

  1. Colors in Matlab
  2. Heatmap in MATLAB
  3. Scatter Plots in Matlab
  4. Complex Numbers in MATLAB
  5. Matlab Comet() | Steps and Methods

MATLAB Training (3 Courses)

3 Online Courses

1 Hands-on Project

8+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More