Quote:
Originally Posted by Duihi77
When is a function a pure function?
In the text, it states on page 173, “The purpose of a method is to perform an animation, but that of a function is to return a value” (p. 173). Later, the text states, “…an animation performed by a method moves, turns, or performs some other action with objects in the world” (p. 173). According to the reading, this is known as altering the state of the world. In testing, I have dually noted you cannot drag any object methods into a function. However, a few paragraphs below, it states, “Section 6-1 introduces the use of functions in methods and explains how to write your own functions as a method that returns a value” (p. 173). Does a method return a value or not? I have not found a way for a method to return a value.
Am I misinterpreting the text?
|
The differences between methods and functions are kind of artificial, some languages make no distinction at all between these concepts. BUT sticking to the conventions will save you a great many headaches. Some of the differences are codified in the way Alice works, like methods not being able to return a value, and some are not. The fact that you can change variables and other things in a function can lead to a breathtaking array of hard to find problems. These changes are called "side effects" because they have nothing to do with the question's answer but do change the environment in some way. Note that this only applies to objects, variables, etc outside the function. Meaning, that if you need to manipulate aspect of the world to arrive at your answer, you can make a variable within the function, give it the worldly value of whatever you need to fiddle with and fiddle on. When the function returns the local variable will go away and the original "worldly value" will be unaffected.
The best general advice I can give is that a function should ONLY answer a question the question may be numeric, logical, or something else (an object, position, ...) but the function should never change the state of the movie. This means that when a function returns its answer nothing should have moved, or changed. The movie is exactly the same as before the function call. You asked a question and got an answer, thats it! If there is any action to be taken that action should be done in the method that called the function and be based on the value the function returned.
This means if something in the movie changes, it is due to something in a method or event not the unplanned change caused by asking a question. I would recommend treating events in a like fashion, except in the simplest case, define a method and put that in the event's slot rather than dumping several lines of code into the event.
Mark