Reputation: 35
I'm a bit confused why the addition function always returns 0 no matter what combination of inputs I place. I've already checked if the user inputs are properly placed within the array I made and there are no problem at all within the input. Is the way I structured the function wrong by any means?
Program MathOperation;
uses crt;
type
inputArray = array [1..5] of real;
var
userChoice : integer;
inputValue : inputArray;
procedure userInputValues;
var
counter : integer = 0;
begin
while counter<5 do
begin
write('>> Enter number [', counter + 1, ']: ');
read(inputValue[counter]);
counter := counter+1;
end;
end;
function addOp:real;
var
addCtr : integer = 0;
sum : real = 0;
begin
while addCtr<5 do
begin
sum := sum + inputValue[addCtr];
addCtr := addCtr+1;
end
end;
Upvotes: 1
Views: 118
Reputation: 30715
Your AddOp
function always returns 0 because you never assign a value to its function result. Somewhere in it, you should have a statement which is guaranteed* to execute and which is like this:
AddOp := {whatever the correct value is}
Since you are apparently using FreePascal, you can use Result
as an alias for the function result, as in
Result := {whatever the correct value is}
*Actually, this is somewhat of an overstatement, because there may be more than one valid execution path through the code of the function, so the more general rule is that every valid execution path through the function should make the function return a value.
Upvotes: 5