Function giving incorrect answer

Hi, new to matlab and I was following through a guide and got to the functions section but I have a problem. Its a very basic function to add two numbers together:
function add (x,y)
x+y
end
In the command window I enter "add 5 3" to which it gives
>> add 5 3
ans =
104
Are there any settings I need to change or something??
I think it is something to do with the variables since addition without the function works perfectly
>> 3+5
ans =
8

 採用された回答

Cedric
Cedric 2013 年 4 月 25 日
編集済み: Cedric 2013 年 4 月 25 日

0 投票

It was almost correct; you just forgot to assign the result to an output argument (that I named "result" below, but you are free to choose whichever valid variable name you want):
function result = add(x, y)
result = x + y ;
end
Then you have to call it as follows in the command line:
>> add(5, 3)

2 件のコメント

joe
joe 2013 年 4 月 25 日
Still have the exact same problem - gives the answer as 104 when it obviously isnt!
joe
joe 2013 年 4 月 25 日
Thank you very much :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

joe
2013 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by