PDA

View Full Version : Boolean Functions


gdev
03-12-2007, 10:39 AM
I have created a Boolean function to do crash detection.

I have:

if (distance between objectes < 1)
then return = true
else return = false

return = true

The last return = true is there from when I first created the function. The problem is when this runs it always returns true thanks to the last return statement. I have an example of something similar from our textbook and they have the last return = none but I cannot find any place that I can set that return to none. Please Help.

DrJim
03-12-2007, 11:19 PM
Here is a start on the answer. (This explaination could really use some more rigor in defining terms and elsewhere - I'd appreciate any help there.)

Your basic problem is that you are trying to use "Return" - which is a reserved word for the value returned from a function - as an ordinary variable in the body of the function itself. This actually will work (which surprises me a bit :rolleyes: ) if you declare a local Boolean variable named "Return" and then "Return 'Return' " (see attached) - but it's a very bad idea.:mad:

A much better appoach is to declare some other local variable name in the function and set up your tests to set its value - then return it using the "Return" line in the function template.

As far as the "Return '<None>' " statement - that is only an allowed option for a function type, such as a texture (again see attached), for which <none> is an allowed value to return for the variable. For a Boolean function, you must (and will be forced by Alice to) ultimately return either a true or false.

gdev
03-13-2007, 06:46 AM
Thanks for the response. I see what you are doing but the problem is that where we are in the textbook we haven't introduced variables yet, and the book tells you to create exactly what I have done and they have a return none. I attatched what the book has done and it works just fine the only problem is that I can't reproduce what they have done. Please help.

DrJim
03-13-2007, 10:04 AM
I can't reproduce it either - once I get rid of the <none> as an arguement for the Return, I can't get it back (which is, in my opinion, the "correct" way for the template to have been set up - but doesn't help you if you are supposed to "duplicate the book").

The text itself (p.159) never actually shows the "Return <none>" line, only the if/else statement. With that if/else statement, the "Return <none> line doesn't really matter, since the function will always return to the calling method before it gets to that line - but I can't explain why your program contains that line or how to duplicate it - nor do I think such a statement should be allowed in a Boolean function - but since it does no harm ???

I though it might be listed as an error in the errata sheet, but the latest reference I have for the errata for the text, http://www.aliceprogramming.net/text/errata.pdf , doesn't list it. Anyone have a newer version or some explaination for this. :confused:

gdev
03-13-2007, 10:32 AM
thanks for the help. The other teacher who teaches this and I have not been able to figure it out at all. I agree that using a local variable makes the most sense but I'm still intrigued why our book decided to add this in before we did variables. Oh well. Looks like I'm introducing variables tommrow in class.

Thanks again.

DrJim
03-13-2007, 09:14 PM
I'm still intrigued why our book decided to add this in before we did variables.

It is intriguing - I'd never really gone throught the Dunn, et al, text from front to back and hadn't realized that variables were treated so late in the book until this question came up. Maybe one of the authors will read this thread and add some insight.

lanceA
03-14-2007, 12:13 PM
I'm not one of the authors, but . . .
In object-oriented programming variables do not carry the same weight they do in procedural programming, hence in most OOP texts the use of variables and loops comes after recursion, etc.

The suggested syllabii on the web site shows various chapter sequencing to be used dependant upon whether or not the teacher wishes to take an objects-first approach or procedural. I believe the Forward to the Dunn, et. al book spells this out also.