MATLAB: Testing a created function

functiontest function

I am trying to write and test a function to determine whether a person is old enough to drink or not (with the legal age being 19).
For some reason when I test the function the code will run but not display the message that I set. How can I fix this code so that it will display the proper message?
FUNCTION:
function[ans] = drink(age)
if(age<19)
ans = 'no';
else
ans = 'yes';
end
TESTING:
clear
clc
addpath('Library')
age = input('Enter your age: ' );
ans = drink(age);
if(ans == strcmpi(ans, 'yes'))
disp('Old enough to drink')
elseif(ans == strcmpi(ans, 'no'))
disp('Not old enough to drink')
end

Best Answer

First, it is best not to use ‘ans’ as a variable, since it is the default variable and can be overwritten.
Second, the the if and elseif conditions were not coded correctly.
This works:
if strcmpi(rsp, 'yes')
disp('Old enough to drink')
elseif strcmpi(rsp, 'no')
disp('Not old enough to drink')
end