# Fahrenheit function but confused on the output.

3 views (last 30 days)
Ireedui Ganzorig on 18 Mar 2020
Commented: Shannon Wagoner on 8 Apr 2020
Hello MATLAB community,
I feel like I did everything right, but the function output is really confusing me. Or did I do everything wrong in the first place?
This is function output.
0 = solid
1 = liquid
2 = gas
Below is my code.
function [state] = h2oState(tempF)
C = input('Please enter your Celsius value to be converted into Fahrenheit ');
tempF = 9/5 * C + 32;
if tempF <= 32
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('solid')
elseif tempF > 212
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('gas')
else
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('liquid')
end
end

Rik on 18 Mar 2020
You are overwriting the input to your function by asking the user for the temperature in Celsius. You are also not assigning any value to your output variable state.
You probably want something like this:
function state = h2oState(C)
tempF = 9/5 * C + 32;
if tempF <= 32
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('solid')
state=0;
elseif tempF > 212
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('gas')
state=2;
else
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('liquid')
state=1;
end
end
Shannon Wagoner on 8 Apr 2020
star