defining a method, the purpose of the brackets

while we are defining a method we write like this
function [ ] = methodsName ()
statement or statements
end
what are the purposes of [ ] and ()
Can you help me? Thanks.

 Accepted Answer

In the brackets, you list all the output variables that you want your function to return. If it returns only one variable, then you can omit the bracket - they're required for 2 or more returned values.
The parentheses contain the input arguments. You list them separated by commas. For example
function theSum = GetSum(a, b)
theSum = a+b;
or
function [theSum, theProduct] = GetSumAndProduct(a, b)
theSum = a+b;
theProduct = a*b;

3 Comments

Thanks a lot it helped a lot. Do you know any link in mathworks that can help me to learn methods
The information about function input and output arguments in function definitions is on this documentation page.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!