27
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Functions, Loops, and Parameters

Alice Workshop Functions, Loops, and Parameters

  • Upload
    sabina

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

Alice Workshop Functions, Loops, and Parameters. Do in order dragon flies to princess princess climbs on dragon's back dragon and princess escape knight shakes his arm (and sword) in protest. dragon takes off. Where are we?. - PowerPoint PPT Presentation

Citation preview

Page 1: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Alice Workshop

Functions, Loops, and Parameters

Page 2: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Where are we? Thus far, we have written object-level methods for the

dragon flapWings

takeOff fly

Do in order

dragon flies to princess

princess climbs on dragon's back

dragon and princess escape

knight shakes his arm (and sword) in protest

dragon takes off

Page 3: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Next

Now, the dragon must fly to the princessBreak it down into simple steps:

Do in order

fly to the princess

dragon turn into a position that will allow princess to climb on board

Page 4: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

World-level method

This method clearly involves two different objects: dragon princess

We will write a world-level method rather than a object-level method.

General rule of thumb: If more than one

object is involved in a method, write the

method as a world-level method.

Page 5: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

turn to face

Page 6: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Loop The fly method moves the dragon forward 1

meter. We want to call the fly method repeatedly to move

the dragon to the princess. Alice has a Loop statement.

Page 7: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Problem

The problem is: How many times should the fly method be called?

?

Page 8: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Functions Alice has several built-in distance functions, that can be

used to determine the number of meters objects are from one another.

In this example, the Loop should execute once for each meter of this distance.

Page 9: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Behind the scenes

To be truthful, Loop needs a whole number (e.g., 4) The distance in front of function returns a

double number (e.g., 4.37 meters)

Behind the scenes, Alice truncates the distance to a whole number

Page 10: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Complete Methods

Page 11: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Animation:Smooth, Continuous Movement

For most purposes, execution of an instruction works best with a gentle slowdown in speed (toward the end of the duration)Successive repetitions, however, may have a smoother continuous motion if made to end abruptly

Page 12: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Animation:Smooth, Continuous Movement

For most purposes, execution of an instruction works best with a gentle slowdown in speed (toward the end of the duration)Successive repetitions, however, may have a smoother continuous motion if made to end abruptly

Page 13: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Object-level method with parameters

We could also write this method for the dragon object

But it still involves two objects in the world dragon princess

We must use a parameter in order to access the princess from the dragon method

Page 14: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Create a object-level method

Page 15: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Parameters A parameter is a shared element that conveys

information between one method and another A parameter, has a name, a value, and a type

Type describes the kind of information that is shared

Click on create new parameter button

Page 16: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Parameters

Name the object "who” (or something else appropriate)

Select the Object type then click OK

Page 17: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Use the parameter

Turn the dragon to face …

Page 18: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

What is a Variable?

A variable is an element in a program to store values that are likely to change as the program executes

Also useful for Naming values that may be abstract Allowing code to be more readable Decomposing a complex process into a

series of simpler processes

Page 19: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Create a variable in a method

Page 20: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Like Parameters

Variables have a name (distanceToWho) Variables have a type (Number)

Page 21: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Parameters vs. Variables

Both parameters and variables Have a name Have a type (which describes the kind of

data being stored) Store values that can change as the

program runsParameters carry values from one

method to anotherVariables belong to one particular

method only (local)

Page 22: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Using a variable

Drag the variable tile into the code editorSelect a value for the variable

Page 23: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Using the function with parameter

To set the value of the variable

Page 24: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Using the variable in the loop

Page 25: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Code

Page 26: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Text

For more information Functions and Expressions

• Chapters 3 and 6 Loop

• Chapters 3 and 7 (Section 1) Methods and Parameters

• Chapter 4 Variables

• 10-1

Page 27: Alice Workshop Functions, Loops, and Parameters

Copyright 2008Wanda Dann, Steve Cooper, Don Slater

Your turn… Write code to have the

princess climb onto the dragon's back (not climb, actually – just move to the dragon)

dragon and princess escape from the castle (use vehicle)

Do in order

princess climbs on dragon's back

dragon and princess escape

knight shakes his arm (and shield) in protest

dragon takes off

dragon flies to princess