Functions Alice.

Slides:



Advertisements
Similar presentations
Class-level Methods Chapter 6. Class-level Method Is specific to a class of objects We can give a class new abilities/methods Only involves this one class.
Advertisements

Review of Chapter 4 Sections 1 and 2 World-level methods involve two or more objects break a large problem into smaller, logical units follow a design.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Programming: Simple Control Structures Part 1 – Conditional Execution Alice.
Introducing While loops (and random numbers too) Alice.
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.
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.
More About Recursion Alice. A second form of recursion A second form of recursion is used when the solution to a problem depends on the ability to break.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Creating An Animation Program Part 2 Alice. Method A segment of program code (instructions) that defines how to perform a specific task.
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.
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.
Creating An Animation Program Alice. Step 1: Design Decide on the problem to be solved Design a solution We will use a storyboard design technique, commonly.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
Programming: Putting Together the Pieces Built-in Questions 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.
Programming: Simple Control Structures
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
Creating An Animation Program Alice. Recall We began the animation creation process We introduced the concept of storyboard We will continue using the.
Programming: Simple Control Structures Sec 46 Web Design.
CompSci 4 Chap 4 Sec 3 Sept 23, 2010 Prof. Susan Rodger.
Class-level Methods and Inheritance Alice. Class-level Methods Some actions are naturally associated with a specific class of objects. Examples A person.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
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.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Parameters Section 8-8 Web Design.
Functions Sec 51 Web Design.
Learn about functions in Alice
Programming: Simple Control Structures
Creating your Function
Programming: Simple Control Structures
Functions Sec 8-11 Web Design.
Parameters Alice.
Programming: Simple Control Structures
Lists Alice.
Lists Alice.
Programming: Simple Control Structures
Parameters and World-level methods
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Programming: Simple Control Structures
Lists Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Lists Alice.
Programming: Simple Control Structures
More About Recursion Alice.
Parameters Alice.
Functions Alice.
Programming: Simple Control Structures
Creating An Animation Program
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Class-level Methods and Inheritance
Functions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Variables and Inheritance A More Complex Example
Presentation transcript:

Functions Alice

Built-in Functions We have been using built-in functions.

How a function works A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.

Types of Functions 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…

A new example A common task in sports game programs is to bounce a ball. To illustrate how this is done, let's bounce the ball over the net. (The ball has been positioned 1 meter from the net.) The ball should move up and forward and then down and forward in a pattern that looks something like this: Note: This looks easy – but do not be deceived!

Design Storyboard A design for a world-level method: World.ballOverNet: Do in order toyball turn to face the net Do together toyball move up toyball move forward toyball move down

Demo ToyballOverNet-V1 ToyballOverNet-V2 Concepts illustrated in this example: Movement of an object is sometimes relative to another object. In this example, a built-in height function is used to determine the height of the net. Then the ball moves up enough to clear the net. the toyball's up and down movements are relative to the ground. An orient to method is used because it is not easy to tell "which way is up." Traditional lecture: demo version 1 and then orient the ball to the ground to show the solution. Active learning: demo version 1 and then have students orient the ball to the ground.

Rolling the ball Another sports game action involves rolling a ball along the ground. We want a realistic motion rather than a slide. The ball must simultaneously move and roll. realisticRoll Do together move ball forward 1 meter turn ball forward 1 revolution

Demo Ch06Lec1ToyballRoll-V1 Ch06Lec1ToyballRoll-V2 Our design assumed that the ball's motion is relative to the ground. But the ball turns relative to it's own sense of direction. Ch06Lec1ToyballRoll-V2 AsSeenBy ground can be used to make the ball turn relative to the ground.

Revising the approach The ball is made to roll 1 revolution. Suppose we want the ball to roll a certain distance (say 3 or 4 or even 10 meters) along the ground. How many times should the ball turn 1 complete revolution?

Number of revolutions The number of revolutions depends on the size of the ball The number of revolutions is distance/( P * diameter) But there is no built-in function to return the number of revolutions We will write our own! one revolution four revolutions

Parameters We want to return the value computed as distance/( P * diameter) Obviously, what is needed is the ball’s diameter the ball object has a built-in width function the distance the ball is to travel can be sent as a parameter to the function

Demo Ch06Lec1ToyballRoll-V3 Concepts illustrated in this example A function must have a return statement to send back a computed value. In this example, a math expression is created to compute a number value. The order of evaluation is indicated by the use of parentheses, as in traditional math expressions.

Calling the function test values Active Learning: Students run the world several times, with different test values. test values We should run the animation with several test values to be sure it works as expected. What happens if you use a negative value?

Levels of Functions As with methods, functions can be either class-level or world-level. (The function just presented was class-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 (built-in world-level functions are fine to use).

Assignment Read Chapter 6 Section1, Functions

Lab Chapter 6 Lecture 1 Lab