How to create a function in MATLAB ? Last Updated : 26 May, 2021 Comments Improve Suggest changes 4 Likes Like Report A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: function output_params = function_name(iput_params) % Statements end The function starts with the keyword function.Returning variables of the function are defined in output_paramsfunction_name specifies the name of the functioninput_params are input arguments to the function Below are some examples that depict how to use functions in MATLAB: Example 1: Function with one output The function calculates the mean of the input vector Matlab % Input vector values = [12, 4, 8.9, 6, 3]; % function return mean of vector c function m = stat(x) n = length(x); m = sum(x)/n; end mean = stat(values) Output : mean = 6.7800 Example 2: Function with multiple outputs The function calculates both nCr and nPr of inputs n and r. Matlab % Input x = 3; y = 2; % Function return p = nPr and c = nCr function [p,c] = perm(n,r) p = factorial(n)/factorial(n-r); c = p*factorial(r); end [p,c] = perm(x,y) Output : p = 6 c = 12 Example 3: Multiple functions in a file stat2() function calculates the standard deviation of the input vector.stat1() calculates the mean of the input vector. Matlab values = [12, 4, 8.9, 6, 3]; % Function returns standard deviation of vector x function sd = stat2(x) m = stat1(x); n = length(x) sd = sqrt(sum((x-m).^2/n)); end % Function returns mean of vector x function m = stat1(x) n = length(x); m = sum(x)/n; end stat2(values) Output : n = 5 ans = 3.2975 Example 4: Function with no input_params In this program, we will create the sin_plot() function that plots the sin() function Matlab % Plotting sin(x) function function sin_plot() x = linspace(0,2*pi,100); y = sin(x); plot(x,y); end sin_plot() Output : Create Quiz Comment M ManikantaBandla Follow 4 Improve M ManikantaBandla Follow 4 Improve Article Tags : Software Engineering Explore Software Engineering BasicsIntroduction to Software Engineering7 min readSoftware Development Life Cycle (SDLC)6 min readSoftware Quality - Software Engineering5 min readISO/IEC 9126 in Software Engineering4 min readBoehm's Software Quality Model4 min readSoftware Crisis - Software Engineering3 min readSoftware Measurement & MetricesSoftware Measurement and Metrics4 min readPeople Metrics and Process Metrics in Software Engineering7 min readHalsteadâs Software Metrics - Software Engineering10 min readCyclomatic Complexity6 min readFunctional Point (FP) Analysis - Software Engineering8 min readLines of Code (LOC) in Software Engineering4 min readSoftware Development Models & Agile MethodsWaterfall Model - Software Engineering12 min readWhat is Spiral Model in Software Engineering?9 min readPrototyping Model - Software Engineering7 min readIncremental Process Model - Software Engineering6 min readRapid Application Development Model (RAD) - Software Engineering9 min readCoupling and Cohesion - Software Engineering10 min readAgile Software Development - Software Engineering15+ min readSRS & SPMSoftware Requirement Specification (SRS) Format5 min readSoftware Engineering | Quality Characteristics of a good SRS7 min readSoftware Project Management (SPM) - Software Engineering8 min readCOCOMO Model - Software Engineering15+ min readCapability Maturity Model (CMM) - Software Engineering10 min readIntegrating Risk Management in SDLC | Set 18 min readSoftware Maintenance - Software Engineering13 min readTesting & DebuggingWhat is Software Testing?11 min readTypes of Software Testing15+ min readTesting Guidelines - Software Engineering3 min readWhat is Debugging in Software Engineering?11 min readVerification & ValidationVerification and Validation in Software Engineering6 min readRole of Verification and Validation (V&V) in SDLC5 min readRequirements Validation Techniques - Software Engineering8 min readPractice QuestionsTop 50+ Software Engineering Interview Questions and Answers15+ min read Like