function [output1,output2,...]=function_name(input_var1,input_var2, ....)
%code is written here
end
For example:
Let's create a function that adds two numbers with the function name add.
function x=add(a,b)
x=a+b;
end
The above function is one output variable and two input variables. x is the output variable and a, and b are input variables.
The function add can be called by the following command:
>>add(5,10)
the output will be:
ans= 15
or,
>>var=add(5,10)
output:
var=15
Similarly, let's create another function with two input variables and two output variables with function name fun2. The function can be defined in Matlab as follows:
function [x,y]=fun2(a,b)
x=a+b;
y=a*b;
end
In the above function, a and b are input variables and x and y are the output variables. The x stores the sum of a and b. y stores product of a and b. The function fun2 can be called using the following command:
>>[out1,out2]=fun2(10,5)
the output will be:
out1=15
out2=50
The function in Matlab can be demonstrated in the following video.
For more articles and video please follow this blog and subscribe my youtube channel: