Suppressing ans in a function

11 ビュー (過去 30 日間)
Omer Acil
Omer Acil 2020 年 5 月 23 日
コメント済み: Omer Acil 2020 年 5 月 23 日
function encStr = encrypt(s,key);
prompt='Enter message to be encrypted: ';
s=input(prompt,'s');
key=input('Enter the key: ');
encStr=char(s+key);
fprintf('The encrypted message is: %s\n',encStr);
end
Hi this is my function above. I don't want to see an ans when I apply this function. I searched it online but most of the answers says that I should get rid of encStr in the function but I must use this format. Can you help me?
Thanks!

採用された回答

Stephen23
Stephen23 2020 年 5 月 23 日
編集済み: Stephen23 2020 年 5 月 23 日
To avoid the ans you must use a semi-colon when you call the function:
str = encrypt(...);
% ^ you need this semicolon!
  3 件のコメント
Stephen23
Stephen23 2020 年 5 月 23 日
編集済み: Stephen23 2020 年 5 月 23 日
"...user may not know that he/she should use semi-colon to suppress it"
Because all variable assignments and function outputs are displayed by default, one of the first things that beginners learn about MATLAB is how to suppress those using a semi-colon.
It is certainly possible to define the function to return no output argument when no output is requested, e.g.:
function out = encrypt(...)
...
if nargout>0
out = encStr;
end
end
but as soon as the user assigns the output to a variable they will need to use a semi-colon to supress displaying it. That is just how MATLAB works. I would recommend that you do NOT mess around with output arguments, as this just results in less predictable function behavior (ultimately your users just have to learn how MATLAB works).
Omer Acil
Omer Acil 2020 年 5 月 23 日
Thank you so much for your time

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by