Octave/Matlab - Function                                      Home : www.sharetechnote.com Function Definition and Execution   In this page, I will explain on how to define a user defined Matlab function (meaning your own function) and how to call (execute) the function.   Function Definition and Execution in a same file - Typical Definition   The most typical way of defining and executing a function is to define a function in a file (*.m) and call (execute) the function in another file (*.m) or command line as in the following examples.   < Example 1 >   Step 1 : Save following code in a file named myAdd.m (Name the file name same as function name function r = myAdd(a,b)    r = a + b;   Step 2 : Save following code in another file (example, fTest.m) c = myAdd(2,4)   Step 3 : Run fTest.m r1 = myAdd(2,4) r2 = myAdd([1 2 3],[4 5 6])   you will get the result as follows.   r1 =        6     r2 =        5     7     9     Function Definition and Execution in a same file   You can define a function and execute the function in a same file as shown in the following example   Step 1 : Create a file as shown below (In this example, I save the code in InFileFunction.m) function main    c = myMultiply(2,4)    d = myDivide(2,4) end   function y=myMultiply(a,b)     y = a * b; end   function y=myDivide(a,b)     y = a / b; end   Step 2 : Run the saved file (InFileFunction.m) and you will get the result as follows. c =        8     d =       0.5000