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.

Slides:



Advertisements
Similar presentations
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Parameters and World-level methods Alice. Our Dragon world The dragon must to take off and fly, to carry the princess.
Repetition Structures
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Fall 2009ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Programming: Simple Control Structures Part 1 – Conditional Execution Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Fall 2007ACS-1805 Ron McFadyen1 Functions and if-else A function is a collection of statement, similar to a method, but a function is defined to return.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
Fall 2007ACS-1805 Ron McFadyen1 Boolean functions & expressions A boolean function is one that returns true or false The guidelines for class-level methods.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 7 Repetition.
Fall 2007ACS-1805 Ron McFadyen1 Ch 7 Loops Alice has two control structures for controlling the repeated execution of statements Loop While.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 3 Programming - Putting Together the Pieces.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 4 OO Programming Concepts.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 6 Functions & If/Else.
Fall 2008ACS-1805 Ron McFadyen1 Programming Concepts Chapter 4 introduces more advanced OO programming techniques. Construction of a programs usually requires:
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Welcome! The Topic For Today Is…Review for Test. Your Topic Basics of Programming Control Statements Programming Concepts InteractionAlice Concepts 200.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Alice: Functions Alice Chapter 6 September 19, 2005.
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Four Fundamental Pieces Instruction Control Structure Function Expression.
© Jalal Kawash Programming Peeking into Computer Science 1.
Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.
What we will do today Learn about functions in Alice.
Functions Alice.
Mathematical Expressions, Conditional Statements, Control Structures
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
Programming: Putting Together the Pieces Built-in Questions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Today’s Agenda 1.Collect Pre-Lab 3 2.Alice Programming Assignment 3.Built-in Functions 4.Expressions 5.Control Structure 6.Assign pair programming teams.
Programming: Simple Control Structures
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
Programming: Simple Control Structures Sec 46 Web Design.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
1 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
Obj: to recognize built-in functions and expressions HW: Read Section 3.1 and do HW4 Edmodo article due Sunday at 8pm Edmodo feedback due 8pm 10/17 Do.
Functions Sec 51 Web Design.
Learn about functions in Alice
Creating your Function
Functions Sec 8-11 Web Design.
Microsoft Visual Basic 2005 BASICS
2-1 Making Decisions Sample assignment statements
Design and Implementation
Pages:51-59 Section: Control1 : decisions
Programming: Simple Control Structures
Functions Alice.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Pages:51-59 Section: Control1 : decisions
Functions Alice.
Functions Alice.
Functions Alice.
Presentation transcript:

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

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)

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…

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

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

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

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

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.

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

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”

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

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

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

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 :

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?

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?

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?

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