Add Functions to Scripts in MATLAB Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report From MATLAB version 2016b, it is possible to add functions directly into a script or live script. In this article, we shall how to add functions to script files. The syntax is simple except one rule that the function body must be written after the codes in the script. statement 1 statement 2 . statement N function 1 function body end function N end Now, let us see the same with the help of some examples. We shall start with a simple function to concatenate three strings. We create a string names geeks.m and add our function to it. Example 1: Matlab % MATLAB code to concatenate two strings str1 = 'geeks'; str2 = 'for'; res=concat(str1,str2,str1); %user defined function to concatenate three strings function str=concat(x,y,z) str=strcat(x,y,z); end Output: Let us create another function to a script, which calculates the binomial coefficient for a given input. Example 2: Matlab % MATLAB code to calculate binomial coefficient of two numbers res=combination(9,7) %user defined function to calculate combination of two numbers function out=combination(n, k) out = factorial(n)/(factorial(n-k)*factorial(k)); end Output: Drawbacks of Adding Functions to a Script:The only drawback of adding a function to a script is that function cannot be used by any other script of the workspace as it is locally defined in a particular script. Comment More infoAdvertise with us Next Article Functions in MATLAB O owl0223 Follow Improve Article Tags : Software Engineering Technical Scripter 2022 Similar Reads Scripts and Functions in MATLAB In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh 2 min read Private Functions in MATLAB Private functions are useful when you want to limit the scope of a function. Here we will learn how to create private functions and also use them. Private functions are primary functions that are visible only to a limited group of other functions. Generally, we make private functions, if we want to 2 min read User defined function in MATLAB Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,. 2 min read Nested Functions in MATLAB Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a Function: To declare a function in MATLAB we use given 2 min read Functions in MATLAB Methods are also popularly known as functions. The main aim of the methods is to reuse the code. A method is a block of code which is invoked and executed when it is called by the user. It contains local workspace and independent of base workspace which belongs to command prompt. Let's take a glance 5 min read Trigonometric Functions in MATLAB In this article, we are going to discuss trigonometric functions and their types in MATLAB. Trigonometric functions are the mathematical functions that can result in the output with the given input. There are six trigonometric functions - Sine (sin)Cosine(cos)Tangent(tan)CoTangent(cot)Secant(sec)Co 5 min read Like