Alice Community  

Go Back   Alice Community > General Discussion > Questions and Comments

Reply
 
Thread Tools Display Modes
Question about documentation...
Old
Duihi77
Junior Member
 
Status: Offline
Posts: 1
Join Date: Nov 2014
Default 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?
   
Reply With Quote
Old
RavenOfCode
Senior Member
 
RavenOfCode's Avatar
 
Status: Offline
Posts: 409
Join Date: Oct 2013
Location: Northern Virginia
Default 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.
   
Reply With Quote
Old
chickentree
Super Moderator
 
Status: Offline
Posts: 250
Join Date: Dec 2012
Location: Frosno, Ca
Default 01-02-2015, 01:21 PM

Quote:
Originally Posted by Duihi77 View Post
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
   
Reply With Quote
Reply

Thread Tools
Display Modes

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 Jump



Copyright ©2023, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.