Why does function create 'ans' output when it is already assigned?

3 views (last 30 days)
a code i've done for my assignment, except upon choosing '0' to exit the while loop, the function creates an 'ans' variable which is unwarranted.
If the entire code is ran inside a script, this does not occur. How may i suppress this? Unless i'm missing something obvious i am puzzled how this is happening. Thank you
Edit: Actual code removed once problem was fixed to avoid future plagiarising of assignment

Accepted Answer

Guillaume
Guillaume on 18 Oct 2016
There are actually two cases where you'll get a ans = display.
The first one is simply caused by the way you call your function, if you do:
>>rps
then when the function ends, you'll get a display of the return value, winner since you haven't terminated the call with a semicolon. This is what happens in the screenshot you showed. To fix that, it's simple, terminate the function call with a semicolon. This has nothing to do with the function code itself:
>>rps; %won't display ans when the function terminates
The second problem is more subtle. In the code, you have:
while player_move ~0
Because of the typo, this is equivalent to:
while player_move
~0 %statement not terminated by a colon, so outputs the result of ~0, which is 1
You of course meant to have
while player_move ~= 0
Note that matlab editors actually highlights the ~ and tells you that there are two problems with the above line. As a rule, if the editor highlights something, you should investigate and fix the issue. It's usually because you've made a mistake.
The other critical section which the editor highlights is your
else winner == 0;
which again does not do what you meant. It's equivalent to:
else
winner == 0; %does not do anything. would have display 0 or 1 if not terminated by a semicolon.
Morale of the story: make sure that there is nothing highlighted in your code.
  3 Comments
Guillaume
Guillaume on 18 Oct 2016
The semicolon at the end of the function declaration is indeed totally unnecessary and does not affect what happens when you call the function.
What matters is the semicolon when you call the function. If you put one at the end of the call statement
>>run;
you don't get a display of the output. If you don't put it
>>run
you do, if the function returns something.
When you click the run button, matlab in effect type the name of your function at the command line without a colon. So you'll always get an output. As far as I know, there's no way to change that (I don't use the run button).
If the fact that you get an output when you don't want one really bothers you, you can solve you by checking the number of output requested, and only creating the output if it's been asked:
function winnerout = rps %semicolon here is not needed and has no effect anyway
winner = nan; %create winner variable so that it exists regardless of code path.
%...
%your code
%...
%at the end of the function
if nargout > 0 %output asked
winnerout = winner; %create output variable
end %note that winnerout is not created if no output asked
end
Ali Tevs
Ali Tevs on 18 Oct 2016
Ah i see. I understand now.
Thank you very much for the detailed explanation, it is sincerely appreciated.
All the best, Ali

Sign in to comment.

More Answers (1)

KSSV
KSSV on 18 Oct 2016
Edited: KSSV on 18 Oct 2016
How about now?
function [winner] = myfunction()
% Initialisation of scores
win = 0;
lose = 0;
draw = 0;
fprintf('What move do you play? \n ');
fprintf('1 - rock, 2 - paper, 3 - scissors \n 0 - quit game \n');
player_move = input('Please make your choice: ');
while player_move ~=0
% error check
while player_move < 1 || player_move > 3;
player_move = input('Invalid! Please make your choice form 1, 2, or 3: ');
end
if player_move == 1
fprintf('you play rock \n');
elseif player_move == 2
fprintf('you play paper \n');
else
fprintf('you play scissors \n');
end
comp_move = randi([1 3]);
if comp_move == 1
fprintf('Computer plays rock \n');
elseif comp_move == 2
fprintf('Computer plays paper \n');
else
fprintf('Computer plays scissors \n');
end
% Creation of a table with outcomes
% [rock(1), paper(2), scissors(3) by rock(1), paper(2),scissors(3)]
% column = player moves, row = computer generated move
% outcome of 1 if player wins, -1 if computer wins, 0 for draw
Rock = [0,-1,1];
Paper = [1,0,-1];
Scissors = [-1,1,0];
table = [Rock; Paper; Scissors];
[winner] = table(player_move, comp_move);
if winner == 1;
win = win + 1;
fprintf('You win! ')
elseif winner == -1;
lose = lose + 1;
fprintf('You lose! ')
else winner = 0;
draw = draw + 1;
fprintf('That''s a draw! ')
end
fprintf('Total: %d win, %d draw, %d lose \n \n', win, draw, lose);
fprintf('What move do you play? \n ');
fprintf('1 - rock, 2 - paper, 3 - scissors \n 0 - quit game \n');
player_move = input('Please make your choice: ');
if player_move == 0
fprintf('Good Bye! \n')
end
end
end
  2 Comments
Ali Tevs
Ali Tevs on 18 Oct 2016
I ran the function and i'm afraid the problem remains.
I suspect it has something to do with the 'while player_move ~=0' statement. As if the function is ran with the user input of 0 initially, the 'ans' output is not generated. but once the while statement is ran, the 'ans' get outputted whenever the statement is exited.
Guillaume
Guillaume on 18 Oct 2016
Siva corrected the cause of ans output of the function (but still left the other bug in your code).
The other cause of ans output is simply due to the way you call the function and nothing to do with the function code itself.
You don't get an output if you input 0 initially because in that case winner has never been assigned, so there's nothing to send to ans. If you input something other than 0, then winner gets assigned a value to when the function returns you get an output. To fix this, call the function with a semicolon (as per my answer).
Note that the above also highlight another problem with your function. If you call it with:
>>result = rps; %with or without semicolon
and input 0 straight away, then you'll get an error because the requested output has never been assigned
Output argument "winner" (and maybe others) not assigned during call to "rps".

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!