variables and functions chapter 3 3-1. variables named storage location in computer’s memory...

20
Variables and Functions Chapter 3 3-1

Upload: britney-wilkinson

Post on 13-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Variables and Functions

Chapter 3

3-1

Variables Named storage location in computer’s

memory Programs may need to store data when

running Types of data that can be stored

o Local variableso Parameter variableso Class-level variableso World-level variables

3-2

Variable Types Local variables

o Belongs to a specific methodo Only used in that methodo Variables cease to exist when method stopso Will be talked about today

Parameter variableso Holds argument that is passed to a called method

Class-level variableso It is like a property and belongs to a specific object

World-level variableso It is a world-level property

3-3

Creating Local Variables Local variables belong to

a specific method (such as world.my first method)

Create variable by clicking on create new variable in desired method

Variables require 3 thingso Nameo Typeo Initial value

3-4

name

type

value

Variable Definition Variable Names

o Must be unique within the method or objecto Should be meaningful and reflect the variable’s

purposeo Named using camelCase

Variable Typeso Numbers, Boolean, Objects, Other (such as

String, Color, Sound, etc.)

Initial Valueo Value initially stored in the variable

3-5

Variables

Tile appearance shows the type of variable

Numbers Boolean Objects String Variable declaration - creating a variable

3-6

Variable Assignment

Variables have an initial value Initial value held until a different value is

assigned New values can be

assigned while the method is runningo Use Set instructionso Done by dragging variable tile into Methods

Editor

3-7

Review What is a variable? What are three things that must be specified

when creating a new variable? Where in the Method Editor do variable tiles

appear? What do the characters 123 appearing on a

variable tile tell you? What do characters abc on a variable tile tell

you? How do you create a set instruction for a

variable? 3-8

Function Asks a question to

o Check a conditiono Compute a value

Used to get information about…o Size of objects

What is snowwoman's height?

o Relationship of one object to another What is distance between lunar robot and rock?

Used to get other informationo Timeo Ask user for information

Value What a function returns after it

o Asks questiono Or performs a computation

Type of value depends on kind of functiono distance to returns a numbero is to the left of returns a Boolean value

Number valueo Whole number ex: 3 meterso Fractional value, ex: 1.2 meterso In lunar example

What is distance of robot to rock?

Function vs. Method Functions

o Perform calculationso Determine state of an object or systemo Return a value

Methods perform an action

3-11

Types of Values Number

o 3, or 1.3 Boolean

o true, false String

o “Slithy Toves?” Object

o lunarRobot, alien Position in world

o (0, 0, 0) – (x,y,z) center of an Alice world

World Level Primitive Functions List of world level functions categories

o Boolean logico Matho Randomo Stringo Ask usero Mouseo Timeo Advanced matho Other

3-13

Asking User for Input

World has three primitive functions that ask for user input

Each question is shown in a dialog box Ask user for a number

User enters a number

Ask user for yes or no User clicks on yes or no

Ask user for a string User answers with words

or a sentence

3-14

Adding Question for Number

Place Answer in Variable

3-16

Class Primitive Object Functions Objects have different

primitive functions from the world level

Class level functionso Proximityo Sizeo Spatial relationo Point of viewo Other

Object primitive proximity functions

Using Function distance to

Built-in distance to function o Determines distance from center of one

object to center of another objecto Better than guessing distance to object

An Alice function is not a "stand-alone" instructiono It is nested within another instruction

Review What makes a function different from a

method? How do you see a list of functions an object

has? What are the three primitive world functions

that ask the user to input a value? What are some class level functions? What does the primitive distance to function

do?

3-19

Homework Read chapter 3 Do tutorials

o Tutorial 3-1 Creating and using a variable, p 116o Tutorial 3-2, Creating a set instruction, p. 118o Tutorial 3-3, Calling an ask user function, p. 122o Tutorial 3-4, Using a proximity function, p. 127

Do lab assignments Due one week after its assigned