In order to create a function in MATLAB, add a couple of lines to your code
function [out_1, out_2, ...
out_k is your output variables from the function ( k = 1:n )
in_k is your input variables which you will pass into your function ( k = 1:n ), if you don't have any input parameters leave the parenthesis blank.
Note: function_name and filename should always be same, otherwise your code will throw an error.
Hope this helps