Top Banner
Creating Functions Deborah Nelson Deborah Nelson Duke University Professor Susan Rodger July 22, 2008
35

Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

May 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Creating Functions

Deborah NelsonDeborah NelsonDuke University

Professor Susan Rodger

July 22, 2008

Page 2: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Loading the WorldLoading the World

• Download the file that we'll be working withDownload the file that we ll be working with today 

• It is named fireStart a2wIt is named fireStart.a2w

• Save it in a directory that you can find again, and then start Alice and open the world NOTE: Youthen start Alice and open the world. NOTE: You cannot double‐click the file to open it; 

• Windows will not know what to use and even if• Windows will not know what to use, and even if you select Alice from a list of programs, the loading will failloading will fail.

Page 3: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Play the WorldPlay the World

• In this world the user is supposed to burnIn this world,  the user is supposed to burn everything that is in the junkyard.

• Play the world• Play the world 

• There is an error. Because whatever the user li k b h i l d hclicks on burns – even the animals and the ground.

• We only want the things in the junkyard to be burned. So we need to write a function

Page 4: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Part 1: FunctionsPart 1: Functions

• Information about the world or the objects inInformation about the world or the objects in the world are all stored in properties

• A function is used to ask questions about• A function is used to ask questions about these properties

F l f h b il i f i f• For example, some of the built in functions of an object are: color, height and depth.

• A function is not a behavior or an action. It simply returns the information that we need.  

Page 5: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

FunctionFunction

• In this world we want to know: is the objectIn this world, we want to know: is the object that was clicked on one of the objects in the junkyard?junkyard? 

• If it is one of the objects in the junkyard, we want the fire to go to itwant the fire to go to it. 

• If it isn’t, we don’t want anything to happen

Page 6: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Types of FunctionsTypes of Functions

• Depending on the question a function canDepending on the question, a function can return any type of value. 

• Some of the options of a return type are:• Some of the options of a return type are:– Number

B l ( f l )– Boolean (true or false)

– Object

– String

• But there are more types

Page 7: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Part 2: Creating a functionPart 2: Creating a function

Step 1: What type of function to createStep 1: What type of function to create 

• We want our function to be type boolean. Boolean returns either true or falseBoolean returns either true or false.

• This function will return true if the object li k d i i h j k d I ill f lclicked on is in the junkyard. It will return false otherwise

Page 8: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 2: How to create a functionStep 2: How to create a function

• Click on the function tab in the world detail’sClick on the function tab in the world detail s pane.

• Click on create new function• Click on create new function

• Name it possibleObject and make sure it is bjtype object

• See the screenshot on the next slide for an illustration

Page 9: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Creating a functionCreating a function

Page 10: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Part 3: Writing the function: Step 1: the parameter

• The object that was clicked on is going to beThe object that was clicked on is going to be used in this function. In the function, create new parameternew parameter

• Name it possibleFuel and it is type object

Page 11: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 2: Writing the function: if/elseStep 2: Writing the function: if/else

• Drag an if/else into your function and selectDrag an if/else into your function and select true in the drop down menu

• Drag the parameter possibleFuel on top of the• Drag the parameter possibleFuel on top of the true

Page 12: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 3: The conditionalStep 3: The conditional

• When you release the parameter selectWhen you release the parameter, select possibleFuel ==;

• then select cardboardBox• then select cardboardBox,

• the entire cardboardBox

• See the screenshot on the next slide for an illustration

Page 13: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008
Page 14: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 4: ReturnStep 4: Return

• Drag return on top of the Do NothingDrag return on top of the Do Nothing

• Select true

Page 15: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 5: Nested if statementsStep 5: Nested if statements

• Drag if/else on top ofDrag if/else on top of the Do Nothing underneath Else

• Then drag possibleFuelon top of the true

• When you release it, select possibleFuel==;

• Select crate

• Select the entire crate

Page 16: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Nested if/else statementsNested if/else statements• Then drag return true onto the Do Nothing. Y f i h ld l k lik hi fYour function should look like this so far:

Page 17: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 6: Finish the functionStep 6: Finish the function• Drag a new if/else onto the Do Nothing underneath Else

• Repeat the steps for these objects:p p j– the teddyBear

– the table

– The oldBed

• Then we want to return false for any object• Then we want to return false for any object not in the junkyard 

• See the screenshot on the ne t slide for the• See the screenshot on the next slide for the rest of the function

Page 18: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

The Rest of the function

• Don’t forget to set the final Return to false

Page 19: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Part 4: Calling the functionPart 4: Calling the function

• Click on the methods tab in the world detailsClick on the methods tab in the world details pane

• Click edit beside the method lite so that you• Click edit beside the method lite so that you can see the code

Cli k h f i b i h ld d il• Click on the functions tab in the world details pane

Page 20: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Calling the function (cont 1)Calling the function (cont 1)

• Drag if/else to the top of the lite methodDrag if/else to the top of the lite method

Page 21: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Calling the function (cont 2)Calling the function (cont 2)

• Now drag the possibleObject from theNow drag the possibleObject from the function tab on top of the true

• Select expressions• Select expressions

• Select objClick

• See the screenshot on the next slide for an illustration

Page 22: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008
Page 23: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

The ifThe if• Click on the already written Do in Order

h f/ l f h• Drag it into the if/else statement on top of the Do Nothing

• Play your world

Page 24: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

The elseThe else

• Scroll down to the Else and drag the cameraScroll down to the Else and drag the camera say method and type in the string “that object shouldn’t be burned”:shouldn t be burned :

Page 25: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Part 5: One more errorPart 5: One more error

• Now when you play your world only theNow when you play your world, only the objects that are in the junkyard are burned. 

• However when you burn one of the objects• However, when you burn one of the objects, you can still burn it again

L ’ h if i h li• Let’s nest another if statement in the litemethod to check whether or not the object h b b dhas been burned– “Burned” means its’ color has been changed to bl kblack

Page 26: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 1: Writing a new function: is it burned?

• Now click on the function tab in the worldNow, click on the function tab in the world details pane. Create a new function named notBurned It should also be type BooleannotBurned. It should also be type Boolean

• Drag an if/else into the function

Page 27: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Is it burned?Is it burned?

• In this function, we want to return true if the objectIn this function, we want to return true if the object has not been burned

• We know an object has been burned if it’s blackj

• Click on cardboardBox in the object tree

• Click on it’s properties tabClick on it s properties tab

• Drag color on top of the true in this function

• Select != blackSelect != black.

• See the screenshot on the next slide for an illustrationillustration

Page 28: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008
Page 29: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 2: Return statementsStep 2: Return statements• Now drag return on top of the first Do Nothing. Select true

• Drag return on top of the second Do Nothing. g p gSelect false. Select false for the last return

• Complete method:Complete method: 

Page 30: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 3: Create new parameterStep 3: Create new parameter

• Create a new parameter in the functionCreate a new parameter in the function

• Name it objClicked and it is type object

bjCli k d f db d i h• Drag objClicked on top of cardboardBox in the function’s if statement

Page 31: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Step 4: Nest an if/elseStep 4: Nest an if/else• Click on the world.lite tab to see the code

f• Drag in a new if statement

• For the Else of that statement, drag in the camera say method and type in: “that object has already been burned”

• So far, your method should look like this:

Page 32: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Nested if/else (cont)Nested if/else (cont)

• Now click on the already written Do in orderNow click on the already written Do in order and drag it on top of the Do Nothing underneath Ifunderneath If

Page 33: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Part 6: Calling the function notBurned

• Click on world in the object tree. Click on the f i b D h f i B dfunctions tab. Drag the function notBurned on top of the true in your if statement

Page 34: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

Calling the function notBurned (cont 1)

• Then drag the parameter objClick onto theThen drag the parameter objClick onto the <None>

• Play your world• Play your world. 

Page 35: Creating Functions - Home | Duke Computer Science · 2008-08-20 · Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008

RecapRecap

• A function is not a behavior or actionA function is not a behavior or action

• It is used to return information about the state of the world or the characters in the worldof the world or the characters in the world

• There are many different types of functions. F l f i bjFor example, functions can return objects, numbers, booleans, etc

• Write your own functions based on questions you need to answer about your world