Functions in a script file
3 ビュー (過去 30 日間)
古いコメントを表示
Hello! I am trying to use a function in a script file to solve a word problem. I have most of the function within the script working but in order for it to work I have to repeat input values inside the function and then prior to calling the function. Is there a way I can eliminate this so the user does not have to type their answer multiple times? The code I have so far is below. Thank you!
while 1 %so there is an unlimited number of runs
gallons=input('Enter how many gallons of gas you need: ');
priceA=input('Enter the price of gas at Station A: ');
priceB=input('Enter the price of gas at Station B: ');
Cost(gallons,priceA,priceB) %calling the function
mpg=input('\nEnter the miles per gallon your car gets: ');
distance=input('Enter the distance between Station A and Station B: ');
B=(distance/mpg)*(priceB); %used to find the amount of money spent traveling to Station B
Total_B=B+(gallons*priceB); %Add to the total spent on gasoline
Savings=(gallons*priceA)-Total_B; %used to find if the user is saving money by subtracting the 2 prices if negative they are losing money
if Savings>0 %positive savings
fprintf('\n Getting gasoline from Station B saves you $%f\n',Savings)
elseif Savings==0
fprintf('\n There is no cost difference\n')
else %losing money
fprintf('\n Buying gasoline from Station A is cheaper\n')
end
function [totalA, totalB]=Cost(gallons, priceA, priceB);
gallons=input('Enter how many gallons of gas you need: ');
priceA=input('Enter the price of gas at Station A: ');
priceB=input('Enter the price of gas at Station B: ');
totalA=gallons*priceA;
fprintf('\n To buy %d gallons of gas at Station A it would cost $%2.2d\n',[gallons,totalA])
totalB=gallons*priceB;
fprintf('\n To buy %d gallons of gas at Station B it would cost $%d\n',[gallons,totalB])
end
0 件のコメント
採用された回答
Guillaume
2017 年 11 月 28 日
It does not look like you understand how functions work. When you write
function [totalA, totalB]=Cost(gallons, priceA, priceB)
gallons, priceA and priceB, within the functions, are variables that automatically receive values from the caller (as long as the caller includes the values in the call). If you then overwrite these values as you've done with the next three lines, these inputs are totally pointless.
Therefore your function should just be:
function [totalA, totalB]=Cost(gallons, priceA, priceB);
totalA=gallons*priceA;
fprintf('\n To buy %d gallons of gas at Station A it would cost $%2.2d\n',[gallons,totalA])
totalB=gallons*priceB;
fprintf('\n To buy %d gallons of gas at Station B it would cost $%d\n',[gallons,totalB])
end
And in your script you call the function with
Cost(something, somethingelse, somethingelseagain);
if you don't care about the outputs of the function or
[output1, output2] = Cost(something, somethingelse, somethingelseagain);
if you want to receive the outputs of the function. something, somethingelse and somethingelseagain can be any variable name. They don't have to match the name of the inputs defined in the function.
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2017 年 11 月 28 日
Delete the lines
gallons=input('Enter how many gallons of gas you need: ');
priceA=input('Enter the price of gas at Station A: ');
priceB=input('Enter the price of gas at Station B: ');
from the Cost function.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!