How to create a function in MATLAB ? Last Updated : 26 May, 2021 Summarize Comments Improve Suggest changes Share Like Article 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 : Comment More infoAdvertise with us Next Article How to Find Number of Function Arguments in MATLAB? M ManikantaBandla Follow Improve Article Tags : Software Engineering Similar Reads How to Insert a Function in Excel? In MS Excel formula is an expression that help to calculate the value of a cell and in Microsoft Excel has many inbuilt in functions that we can use in our formula. Now if you want to insert a function in Excel or want to see all the functions by category, then in this series of MS Excel tutorial gu 4 min read Creating Function in Files in MATLAB MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Functions:The function is a set of statements or commands, which take input/s as parameters and return output. We write functions to 2 min read Alternatives To Eval Function in MATLAB The eval function is one of MATLAB's most powerful and flexible commands. Eval is an acronym for evaluating, which is exactly what it does: it evaluates MATLAB expressions. Any command that can be executed from the MATLAB prompt may be executed from a Mfile using eval. Use of Eval Function:The Eval 3 min read Function Argument Validation in MATLAB The basics of Function Argument is, " The functions receive certain inputs from the calling command which applies in function syntax and processes it. sometimes It may or may not produce output argument it dependence completely on the functions efficiency or the accuracy of your code." Function Argu 5 min read How to Find Number of Function Arguments in MATLAB? The number of function arguments passed in MATLAB will be determined in the following article. Unlike C, C++, and Java, MATLAB can accommodate a variable amount of parameters provided into a function without throwing an error. We'll go through how we determine the actual amount of parameters supplie 4 min read Filter Function in MATLAB The filter function or 1-D digital filter is a function in MATLAB that is used to filter a given noisy data by removing the noise in the data and sharpening or smoothing the input function. As MATLAB provides a dedicated Signal Processing Toolset, the filter function comes handy to remove noise from 3 min read Like