Asked by rihan jericho
on 30 Jan 2018 at 13:02

for example i want to calculate the area of a triangle. so i will use the formula 1/2(b*h). and i also want to calculate volume of a triangle and i will use 1/2(b*h*l). so when i am passing the base(b) and the height(h), i want it to give me the area, and when i am passing the base(b), height(h) and length(l) i want it to give me the volume. i want to use function for my input arguments. How can i do it?

Answer by Jan Simon
on 31 Jan 2018 at 10:22

Accepted Answer

A small example code:

function r = Calculate(a, b, c) switch nargin case 2 disp(a) disp(b) r = a * b; case 3 disp(a) disp(b) disp(c) r = a * b + c; otherwise error('2 or 2 inputs required'); end

If you do not know how many inputs the function will get, use `varargin`. See

doc nargin doc varargin

Log in to comment.

Answer by John D'Errico
on 30 Jan 2018 at 13:06

Edited by John D'Errico
on 30 Jan 2018 at 13:08

Learn to use nargin, so you can test to see how many arguments were provided. Then you can put a branch in to do the calculation as you want.

That is, you can write a function to have three arguments, but if only two arguments were provided when you call it, MATLAB assumes it is the first two arguments. So nargin allows you to differentiate in how the function was called.

Log in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## John D'Errico (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379757-i-am-trying-to-write-a-function#comment_530366

Are you asking people to write a function for you, thus doing your work for you? Why not start be reading the help about functions? Read the help about nargin? Learn to use is, so test the value of nargin.

Problems like this try to push you to read the documentation. To learn to use the tools in MATLAB. But if you don't bother to make an effort, you will never learn to use MATLAB.

## Jan Simon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379757-i-am-trying-to-write-a-function#comment_530416

@rihan jericho: Unfortunately you have removed essential parts of your question. Now e.g. my answer seems to be meaningless. The idea of the forum is to share questions and answers in public. Therefore all questions concerning Matlab are welcome.

Do not take it personally if you are reminded to take a look into the documentation. Sometimes newcomers are not aware, that Matlab's documentation is really useful and clear, because they have seen too many other rather confusing docs of other software. Therefore the members of the forum try to encourage beginners to read the manuals. But this is not a critic.

Your original question concerned a function with a different number of inputs. Does my answer help you to solve the problem? Now you are asking for the calculation of the area of a triangle. What is your problem with this exactly? Please do not change the topic of a question, if you got answers already. Thanks.

## John D'Errico (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379757-i-am-trying-to-write-a-function#comment_530429

When you delete your question, you insult the person who spent the time to try to help you. You make their answer useless to anyone else.

An by posting homework questions, and then deleting them you are in effect asking someone to help you to cheat.

And this is why I will never answer another of your questions again.

## John D'Errico (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379757-i-am-trying-to-write-a-function#comment_530436

Why do we care that you have finished your homework? Answers is here to help others too. They might have been able to learn from your question.

You never should have posted the question in the first place if you were attempting to cheat on your homework. And if your teacher would have been unhappy at seeing the question, then you were indeed cheating.

And, yes, I do remember that you said your teacher would kill you if they saw your question posted here. Deleting the comment does not delete it from my memory.

Personally, I do hope that your teacher happens to see this question, and then starts asking questions of you as to what the question was. Anyway, they can surely recognize what you asked, merely by reading the answers. So my fervent hope is that you will sill be unmasked as a person who is quite willing to cheat on their work. Sorry, but eventually your actions will catch up to you unless you start to learn personal responsibility.

As I said before, all you do is ensure that we will avoid answering further questions by you, or close them when we recognize them for what they are.

## Jan Simon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379757-i-am-trying-to-write-a-function#comment_530479

@rihan: Your question can be found in Google's cache, even if you delete it here:

It is no cheating, if you ask for hints and not for a solution. Why should your teachers care as long as you do not try submit a solution written by someone else? Homework questions are welcome in this forum, as long as they are explicitly marked as homework and contain a specific question. Posting the current code and asking for assistance is not cheating. See https://www.mathworks.com/matlabcentral/answers/8626-how-do-i-get-help-on-homework-questions-on-matlab-answers.

But you have deleted the contents of several of your questions now. This is not a fair usage of the forum. John is right: The members of the forum will avoid answering. And they are smart enough to recognize, that you change your account name repeatedly.

Log in to comment.