Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to.

Similar presentations


Presentation on theme: "Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to."— Presentation transcript:

1 Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to the point where it is called. Many built-in functions We can also define our own. Last statement in a function is a “return” statement. Methods are designed to ask objects to perform actions. Functions are designed to inform us regarding some aspect of the state of the world

2 Fall 2009ACS-1805 Ron McFadyen2 Functions A function is said to accept a number of inputs (argument values passed to parameters) and produces a single output (the return value)

3 Fall 2009ACS-1805 Ron McFadyen3 Types The type of a function depends on the type of value it returns. a number a specific object a color a boolean (true or false) other property values…

4 Fall 2009ACS-1805 Ron McFadyen4 Example: Rolling a ball A common sport action involves rolling a ball along the ground. We want a realistic motion. The ball must simultaneously move and roll. realisticRoll Do together move ball forward 1 meter turn ball forward 1 revolution BallRolling.a2w

5 Fall 2009ACS-1805 Ron McFadyen5 Number of revolutions The number of revolutions depends on the size of the ball The number of revolutions is distance/(  * diameter) But there is no built-in function to return the number of revolutions one revolution four revolutions

6 Fall 2009ACS-1805 Ron McFadyen6 Parameters We want to return the value computed as distance / (  * diameter) We need the ball’s diameter there is a built-in width function the distance the ball must travel

7 Fall 2009ACS-1805 Ron McFadyen7 Level As with methods, functions can be either class-level or world- level. The guidelines for class-level methods also apply to class- level functions: No references to other objects. No references to world-level functions you have written, but built-in world-level functions are fine to use

8 Fall 2009ACS-1805 Ron McFadyen8 if-else An if-else is a decision control structure used to select one set, or another set, of instructions for execution.

9 Fall 2009ACS-1805 Ron McFadyen9 Boolean functions & expressions A boolean function is one that returns true or false The guidelines for class-level methods also apply to class- level functions: No references to other objects. No references to world-level functions you have written, but built-in world-level functions are fine to use

10 Fall 2009ACS-1805 Ron McFadyen10 Boolean expressions Expressions like a==b test two operands and return a value of true or false. Boolean expressions are used in while, if expressions, … A boolean function has returns like: Boolean operators not, and, or can be expressed in tabular form a!=b means “a not equal to b”

11 Fall 2009ACS-1805 Ron McFadyen11 Boolean expressions These are simple boolean expressions that are either true of false What is the phrase: possiblePhilosopher = who ?

12 Fall 2009ACS-1805 Ron McFadyen12 Boolean expressions Not operator anot a truefalse true

13 Fall 2009ACS-1805 Ron McFadyen13 Boolean expressions And operator ba and b true false a true false

14 Fall 2009ACS-1805 Ron McFadyen14 Boolean expressions Or operator ba or b true falsetrue a false … In alice, we can compose boolean expressions that are nested :

15 Fall 2009ACS-1805 Ron McFadyen15 Boolean expressions The text has a couple of examples on page 190: 1. either (both(who==homer) and (who.color==blue)) or (who.color==black) or both Consider: And then we develop: How is this interpreted, how is it developed?

16 Fall 2009ACS-1805 Ron McFadyen16 Boolean expressions The text has a couple of examples on page 190: 2. both(who==homer) and (either who.color==blue or who.color==black or both) Consider: And then we develop: How is this interpreted, how is it developed?

17 Fall 2009ACS-1805 Ron McFadyen17 Boolean expressions The text has a couple of examples on page 190: 1. either (both(who==homer) and (who.color==blue)) or (who.color==black) or both 2. both(who==homer) and (either who.color==blue or who.color==black or both) Are they the same? Are they different?

18 Fall 2009ACS-1805 Ron McFadyen18 Random Functions There are two random functions in alice: 1.Choose true …. Returns a boolean value 2.Random number Returns a number as you prescribe


Download ppt "Fall 2009ACS-1805 Ron McFadyen1 Functions A function is a collection of statement, similar to a method, but a function is defined to return a value to."

Similar presentations


Ads by Google