EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Create Function
 

Matlab Create Function

Priya Pedamkar
Article byPriya Pedamkar

Updated May 31, 2023

Matlab-Create-Function

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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)

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

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.

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–

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW