1 / 20

Creating Functions

Creating Functions. Pepper. Homework. Asking the user: Drag variable to create a set statement Drag a world function NumberDialog or StringDialog over the value Speaking a number Drag a character’s say method to create a say statement Drag the world’s to string function

vita
Download Presentation

Creating Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating Functions Pepper

  2. Homework • Asking the user: • Drag variable to create a set statement • Drag a world function NumberDialog or StringDialog over the value • Speaking a number • Drag a character’s say method to create a say statement • Drag the world’s to string function • Drag the variable into the world’s to string function

  3. Practice now • Create a movie with one character • Ask name • Say name • If time – • Ask number • Say number

  4. Ask name movie solution It would be great to have one balloon with “oh, my name is xxxx” • Use world string function a + b • Do that now – • Click world, then function • Drag a+b over “oh my name is “ string • Select bunnyName variable

  5. Ask Name solution 2

  6. Functions we have used World User question for number World to String Bunny Distance to

  7. Why a function? • Want something done – but you want an answer back • Example: • Calculate an order price • Give the user a screen of questions and pass back whether they hit ok or cancel • find out whether a move is valid (must give some info) • Find out what should happen when the user hits a guardrail

  8. Create your own function • Purpose – tell me how far will be close to the horse • using the rule: give me 90% of the way to the horse

  9. How? • Add a horse to your world • Click the bunny • Click function and then new function , named getCloseDistance • Return a number • Click new variable closeDist • Drag down variable to make a set statement • Drag distance to over the set value and then choose horse • Drag down the variable again to make another set statement and choose expression / variable /math * .90 • Drag the variable into the return statement

  10. getCloseDistance • Now, use this function in your movie:

  11. New function questions Which command used the function? Why did you need a variable in this case to put the function into? Can you use this function without putting it into a variable?

  12. Function vs method • How is a function different than a method? • How is a function the same as a method?

  13. Function with Parm • I want the main program to control how close – send the % • How? • Create new parm in function • Use parm instead of .90 • Change main program to send .90 • Call with .50 on first line and then with .90 on last line

  14. Solution

  15. Add parm of which object • Control which object it moves to in the main program

  16. Solution

  17. Make your own function 2 • to have your character ask the user their first name and last name and return the full name. • (use a+b world function to put together full name) • Create the function: • Click on character • Click on function • Click create new function returning a string • In the function: • Click new variable for first name • Click new variable for last name • Ask user for first name • Ask user for last name • Return first + last (dragging over to what)

  18. New function solution

  19. Property Values • Use – drag to set color • Would be great for characters to know their names • Ask and put answer into self • Change your program to save the name into self • Say their name at the end of the program

  20. Homework Have finished reading chapter 1, 2 & 3 Each person should add 2 methods to any one character. 1) a whoAmI function that will ask the user for his first name and last name and return the full name. 2) a sayHello method that accepts a name parameter and will say "My name is " and the name parameter and "I have a movie for you." When the methods have been added, please upload your character to the discussion board. Then, one person can create a movie with these characters. Create a variable for each character's name. Then it should ask the user to name the characters (meaning set variable to the whoAmI function). Then, it should call the sayHello method on each character, passing in their name. Only one person needs to upload. Please name the world file with both people's names. ex: kris_pepper_and_mary_smith.a2w

More Related