Board index » delphi » boolean variable in function

boolean variable in function

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. It does
comile into an executable but doesnt satisfy my requiremtns of:

Mark<0 or Mark>100 : output error message                   this condition
is satisfied
OR mark>50                : output student passed=TRUE      this condition
doesnt work
Mark<50                      : output studnet passed=FALSE    this condition
works

Thanks
Neerav :-]

Webmaster of the Adextinguisher homepage.
Adextinguisher is a freeware banner ad blocking program
located at http://www.crosswinds.net/~neeravb/adext

PROGRAM test2 (input,output);

VAR
  mark : integer;
  Pass : boolean;
  Passed: boolean;

FUNCTION CalcResult ( value:real ; result:boolean) : boolean;
BEGIN
  IF mark>50 THEN
    result := TRUE
  ELSE IF mark<50 THEN
    result := FALSE;
  Writeln ('Student passed= ',pass:5);
END;

BEGIN
  pass :=false;
  WRITELN ('Please enter students mark');
  READLN (Mark);
  IF (mark>100) OR (mark<0) THEN
    Writeln ('mark must be between 0 and 100 inclusive')
  ELSE
    Passed := Calcresult(Mark,pass);
END.

 

Re:boolean variable in function


Quote
Neerav wrote:
> 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. It does
> comile into an executable but doesnt satisfy my requiremtns of:

> Mark<0 or Mark>100 : output error message                   this condition
> is satisfied
> OR mark>50                : output student passed=TRUE      this condition
> doesnt work
> Mark<50                      : output studnet passed=FALSE    this condition
> works

> PROGRAM test2 (input,output);

> VAR
>   mark : integer;
>   Pass : boolean;
>   Passed: boolean;

> FUNCTION CalcResult ( value:real ; result:boolean) : boolean;
> BEGIN
>   IF mark>50 THEN
>     result := TRUE
>   ELSE IF mark<50 THEN
>     result := FALSE;
>   Writeln ('Student passed= ',pass:5);
> END;

This function does not return a value. You must assign the return value
to the function name:

CalcResult := result.

A shorter variant of this function is:

function CalcResult(value: real): boolean;
var
  result : boolean;
begin
  result := mark > 50;
  writeln('Student passed = ', result);
  CalcResult := result
end;

Note: local variables should be declared in a var-section, not in
the procedure header.

Quote
>     Passed := Calcresult(Mark,pass);

See above.

Re:boolean variable in function


Neerav wrote in
<39360f71$0$8439$7f31c...@news01.syd.optusnet.com.au>...

Quote

>Mark<0 or Mark>100 : output error message                   this
condition
>is satisfied
>OR mark>50                : output student passed=TRUE      this
condition
>doesnt work
>Mark<50                      : output studnet passed=FALSE    this
condition
>works

>PROGRAM test2 (input,output);

>VAR
>  mark : integer;
>  Pass : boolean;
>  Passed: boolean;

>FUNCTION CalcResult ( value:real ; result:boolean) : boolean;
>BEGIN
>  IF mark>50 THEN
>    result := TRUE
>  ELSE IF mark<50 THEN
>    result := FALSE;
>  Writeln ('Student passed= ',pass:5);
>END;

>BEGIN
>  pass :=false;
>  WRITELN ('Please enter students mark');
>  READLN (Mark);
>  IF (mark>100) OR (mark<0) THEN
>    Writeln ('mark must be between 0 and 100 inclusive')
>  ELSE
>    Passed := Calcresult(Mark,pass);
>END.

When you are writing a computer program, you just HAVE to be VERY
pedant in details.  I start from you main program as the program does
itself.

Your first condition is clearly satisfied.  It has nothing to do with
boolean variables or functions, it is just a simple and clear boolean
expression.

If the first condition is not satisfied, you call the function.  In
the function you have an unused parameter "value", which strangely is
of type real.  You just haven't been thinking and you haven't checked
your code pedantically.  But this is not really an error, it doesn't
prevent the code to be compiled or executed correctly.

You also pass the variable "pass" to the parameter "result".  Okay,
but what are you doin' with it?  When you pass it, it will always be
false, because you set it to false in the beginning of the program.
So why to pass it at all?  The variable "mark" is the key value to
effect the function.

Then you correctly test for mark being greater than 50 and set result
according to it.  Okay.  Then you have ELSE and another IF?  This
function is called only if mark is in the range 0..100.  So, if it
isn't greater than 50 then it must be lower OR EQUAL to 50, so you
don't have to test it anymore.  And, note that, you have forgot the
value 50 totally.

Now, that you have set the variable (unnecessary parameter) result
according to the value of mark, you print out the variable PASS!  And
it has all the time been false, so you get FALSE.

Finally, you have a function, but you don't pass any result value out
of the function.  Before exiting the function, you should set the
value of it with a sentence something like this:

    Calcresult := {value of the function};

--
Raimo Suonio, Helsinki, Finland
http://www.dlc.fi/%7Eexp-1/suonio/
Oikeinkirjoitusohjeita news- ja web-kirjoittajille:
http://www.dlc.fi/%7Eexp-1/oikeinkirjoitus.html

Re:boolean variable in function


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.

You're overdoing it a bit.

The result of a function is transferred trough it's
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 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.

Re:boolean variable in function


Re:boolean variable in function


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.

Re:boolean variable in function


Re:boolean variable in function


In article <8h5lhi$a0...@news.iaehv.nl>,
Huub van Dooren <hvdoo...@iae.nl> wrote:

Quote

>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"

Pardon???? What do you say. Please test before replying.

Osmo

Re:boolean variable in function


In article <39360f71$0$8439$7f31c...@news01.syd.optusnet.com.au>,
  "Neerav" <NB-N...@usa.netREMOVEME> wrote:
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. It
does
> comile into an executable but doesnt satisfy my requiremtns of:

> Mark<0 or Mark>100 : output error message                   this
condition
> is satisfied
> OR mark>50                : output student passed=TRUE      this
condition
> doesnt work
> Mark<50                      : output studnet passed=FALSE    this
condition
> works

> PROGRAM test2 (input,output);

> VAR
>   mark : integer;

    ^^^^ is integer but you're passing it into a function that expects a
"real" variable

Quote
>   Pass : boolean;
>   Passed: boolean;

    ^^^^^^ one of these is not needed
Quote

> FUNCTION CalcResult ( value:real ; result:boolean) : boolean;

                              ^^^^ does this really need to be a "real"
variable?  Unless "value" can be something like 72.8 it should probably
be declared as a byte (since it will only be in the range 0..100).

Quote
> BEGIN
>   IF mark>50 THEN

       ^^^^ you've passed a variable into "value" but you're checking
"mark".

Quote
>     result := TRUE

      ^^^^^^ this is not being used, nor is it needed.  You should be
returning the result with the function name (that's what functions are
for).

Quote
>   ELSE IF mark<50 THEN

What if the mark = 50?

Quote
>     result := FALSE;
>   Writeln ('Student passed= ',pass:5);

    ^^^^^^^ the whole idea behind using a function is so you can return
a value back to the program that called it; that program then acts on
the value.  If you're going to act on the result here, there's no need
to make this a function.  Ideally, though, this line should be put in
the main program.

Quote
> END;

> BEGIN
>   pass :=false;

    ^^^^^^^^^^^^ this isn't needed

Quote
>   WRITELN ('Please enter students mark');

    ^^^^^^^ try "write" instead of "writeln" here

Quote
>   READLN (Mark);
>   IF (mark>100) OR (mark<0) THEN
>     Writeln ('mark must be between 0 and 100 inclusive')
>   ELSE
>     Passed := Calcresult(Mark,pass);
> END.

This will only check once for a valid mark.  Consider putting this in a
"repeat" or "while" loop.

A much simpler function would be:
function CalcResult(value : byte) : boolean;
  const PASSED = 50;
begin
     CalcResult := value >= PASSED;
end; (* CalcResult *)

--
Get paid to surf:
www.alladvantage.com/go.asp?refid=HAM962

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:boolean variable in function


Osmo Ronkanen heeft geschreven in bericht
<8h5ti6$1g...@kruuna.Helsinki.FI>...

Quote
>In article <8h5lhi$a0...@news.iaehv.nl>,
>Huub van Dooren <hvdoo...@iae.nl> wrote:
>Pardon???? What do you say. Please test before replying.

>Osmo

Look it up in the manual yourself, it's somewhere under
"Extended Syntax". Then get lost and do not bother me
with it.

Huub.

Re:boolean variable in function


Quote
Huub van Dooren wrote:
> Look it up in the manual yourself, it's somewhere under
> "Extended Syntax". Then get lost and do not bother me
> with it.

You mean @result, maybe?

Re:boolean variable in function


In article <8h632f$hv...@news.iaehv.nl>,
Huub van Dooren <hvdoo...@iae.nl> wrote:

Quote
>Osmo Ronkanen heeft geschreven in bericht
><8h5ti6$1g...@kruuna.Helsinki.FI>...
>>In article <8h5lhi$a0...@news.iaehv.nl>,
>>Huub van Dooren <hvdoo...@iae.nl> wrote:

>>Pardon???? What do you say. Please test before replying.

>>Osmo

>Look it up in the manual yourself, it's somewhere under
>"Extended Syntax". Then get lost and do not bother me
>with it.

I asked you to test it. Apparently you did not. The closest I can think
is that in assembler blocks one uses @result to set the return value.

Osmo

Re:boolean variable in function


Osmo Ronkanen heeft geschreven in bericht
<8h673l$c5...@kruuna.Helsinki.FI>...
Quote
>I asked you to test it.

{
It might help if you wrote:
I just tested your statement and it said 'Unknow identifier'
Are you sure about your "Result:=........"?
I'm not a dog, you know
Quote
}
>Osmo

Well, allright. Just did.
Conclusion: Used to be a valid construction in TP5, in
TP7 it is not. Sorry, can't test with TP5 right now;
computer hangs.

Huub.

Even programmers with big ego's can occassionally
learn something new.

Re:boolean variable in function


In article <%_xZ4.2814$J5.146998@stones>,

Quote
Peter Gates <p...@NOcdbaseSPAM.co.uk> wrote:
>> I asked you to test it. Apparently you did not.
>Er, and he should do exactly what you say all the time? Especially when he's
>correct!

>> The closest I can think is that in assembler blocks one uses @result to
>set the
>> return value.
>Follow your own advice and try it, 'result' works fine and many people
>(myself included) prefer it as it both makes it expressly clear what you are
>doing even if you're deep in someone else's code and is also capable of
>being used on the 'right side' of a statement to obtain the currently set
>return value.

Of course I tried it. And it failed.  It just gives an error or
undefined identifier.  And I am using extended syntax.

Osmo

Re:boolean variable in function


Quote
Peter Gates wrote:
> Follow your own advice and try it, 'result' works fine and many people
> (myself included) prefer it as it both makes it expressly clear what you are
> doing even if you're deep in someone else's code and is also capable of
> being used on the 'right side' of a statement to obtain the currently set
> return value.

I wonder what you are smoking.

function hi: boolean;
begin
  result := true
end;

This will obviously NOT compile under BP 7, therefore Huub's statement
that

"[...] 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' "

is plain wrong. Period. Feel free to prove me wrong by providing
code that makes use of this "feature" and actually compiles.

Go to page: [1] [2]

Other Threads