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.
Best Answer