Neerav heeft geschreven in bericht
<39360f71$0$8439$7f31c...@news01.syd.optusnet.com.au>...
Quote
>Hi
>The following is a litte program that i'm trying to write in TP
5.5 to
>better understand the use of Boolean variables inside functions.
Neerav heeft geschreven in bericht
<39360f71$0$8439$7f31c...@news01.syd.optusnet.com.au>...
Quote
>Hi
>The following is a litte program that i'm trying to write in TP
5.5 to
>better understand the use of Boolean variables inside functions.
(Disregard the previous posting... Outlook Express is playing
some (dirty) tricks on me. It should read:)
You're overdoing it a bit.
The result of a function is transferred trough its
function-name. In the last line of Calcresult you
should write CalcResult:=.... (boolean expression).
But in TP7 (and maybe in TP5.5) the word "result"
is also used as a reference to that function name.
Thus writing "Result:=true" means the same as
"CalcResult:=true"
So to stop the confusion: your result-parameter is
now to be known as: res1.
If CalcResult was a procedure you could pass
the result of it through res1. But if you want the value
of res1 become to be known to the outside of
CalcResult you have to write it as a var-parameter:
procedure CalcProc(mark: integer; var res1:boolean);
begin
....
res1:=......
end;
and to call it:
CalcProc(Mark,Passed);
Notice that res1 is only a name to refer to within the
procedure, but that variable Passed is actually used
the hold the boolean value.
Tip: use different names for parameters and for
variables passed as actual values trough that
parameters, it avoids even more confusion.
But to call a function you write:
Passed:=CalcFunc(Mark);
And you now see that there is no extra parameter
needed to pass the result (of the boolean variable)
to the main program.
And if you want to stick to the "pure" definition
of function and procedure: A procedure _does_
something and a function _delivers_ a value.
(So a function is not supposed to have a
writeln-statement in it.)
The use of IF:
If you have:
if (Mark>50)
Then (Mark>50) is either true or false. If it's true then
Mark is in the range 51..99. If it's not then Mark is
(automatically) in the range 1..50.
So if you forget about Mark=50 for a while (since you do
not use it) then the second IF after THEN is not needed.
Now if the function CalcFunc delivers a boolean and
(Mark>50) represents a boolean value, then you can
directly assign it to the function-name.
function Is_passed(mark1:integer):boolean;
begin
Is_passed:=(mark>50)
end;
function Is_in_range(mark1:integer):boolean;
begin
Is_in_range:=....... {you can figure this out}
end;
Tip: I always start names of boolean functions
with "Is_....." so its name directly indicates it
is a function rather then a procedure.
Struggle some more and you get the hang of it.
Greetings. Huub.