Duihi77 Junior Member   Status: Offline Posts: 1 Join Date: Nov 2014 Question about documentation... - 01-01-2015, 08:21 AM 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?
 RavenOfCode Senior Member     Status: Offline Posts: 409 Join Date: Oct 2013 Location: Northern Virginia 01-01-2015, 09:57 AM A method performs an action, a function performs an equation. Ex. Method makes the bunny hop, Function sees how close the bunny is to the car. As you can see in Alice you cannot place actions in a function, buy in methods simple functions like distance from something are generally put in a method. I hope this helps in not ill clarify more with a world. Stuff + Other Stuff + Different Other Stuff = Things :) My best Alice game: Clash of the Cubes (an arena fighting game): http://www.alice.org/community/showthread.php?t=10738&highlight=game Last edited by RavenOfCode; 01-01-2015 at 02:59 PM.
chickentree
Super Moderator

Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
01-02-2015, 01:21 PM

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

Mark Henwood
mhenwood@ieee.org

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     Community News and Announcements Alice 3     How Do I?     Works In Progress     Share Your World     Share Custom Classes     Bugs and Trouble Shooting     Suggestion Box Alice 2     How do I...?     Works-In-Progress     Share Worlds     Share Objects     Bugs and Troubleshooting     Suggestion Box Educators     Teaching with Alice     Teaching Using Alice 3     Teaching Using Alice 2     CS Principles and Alice     AP CSA and Intro to Java and Alice     Workshops     Alice Player and VR General Discussion     Questions and Comments     The Lounge