From IPRE Wiki
Jump to: navigation, search

Lab 2: Function Function What's my Function


  • Learn how to write functions
  • Functions with parameters
  • Functions without parameters

Lab Exercises

As we saw in Lab #1, you can create a function that will do a sequence of things for you. We used a function like:

def romeo(message):
    setVoiceName("English-US Male")

def juliete(message):
    setVoiceName("English-US Female")

Which was called like:

romeo("Where are you, honey?")

What does this do exactly?

  1. def romeo(...) defines a function named romeo
  2. def juliete(...) defines a function named juliete
  3. The functions both takea one argument, called message
  4. When you enter romeo("Where are you, honey?") that calls the function romeo with the argument "Where are you, honey?".
  5. "Where are you, honey?" gets passed to the romeo function, and message then represents the string "Where are you, honey?"
  6. Wherever you mention message inside the romeo function, it is shorthand for "Where are you, honey?"
  7. Calling the function does the following, in order:
    1. sets the voice
    2. speaks the message

In this manner, you can re-use the functions over and over, once it is defined:

romeo("Here I come!")
juliete("I'm not ready!")

You don't really need functions; you could do the above without them:

setVoiceName("English-US Male")
setVoiceName("English-US Female")
setVoiceName("English-US Male")
setVoiceName("English-US Female")
setVoiceName("English-US Male")
setVoiceName("English-US Female")
setVoiceName("English-US Male")
speak("Here I come!")
setVoiceName("English-US Female")
speak("I'm not ready!")

So, functions can be used to turn repetitive steps into a named sequence of steps (like romeo, and juliete).

Functions can be used to take strings (as per above) or other kinds of things, such as numbers. Let's write a function, named add, that will take two numbers and add them together:

def add(a, b):
    return a + b

You can call it using:

python> add(6, 7)

Something new: the return statement. Use this if you want the function to return back with this information to where it was called. In this manner, add(6, 7) actually means 13, and you can use add(6, 7) where ever you would have used the number 13 before in Python. Examples:

python> add(6, 7) + 100

In fact, you can use add, in many ways, once it is defined you can do the following:

add(1, 2) + add(3, 4)
add( add(100, 200), add(300, 400) )
add( -1, 1)
add( add( add(1, 2), add(3, 4) ), add(5, 6))

Question #1: describe what the above examples do, and what are the results of each.

You can define functions that take zero, or more arguments:

def one()
   return 1

def two():
   return 2

These can be combined with the previous functions:

python> add( one(), two() )

Calling a function means putting an open paren right after a functions name, followed by zero or more arguments, followed by the closing paren.

Question #2: define the functions multiply and divide that do the right things, and return the result. Demonstrate that they work.

As we have seen with romeo, functions can also work on strings:

def concat(string1, string2):
    return string1 + string2

speak( concat("Hello", "World"))
Question #3: Try the above concat code. Does it work correctly? Does
speak( concat("Hello", concat(" ", "World")))
work better? Why?

Question #4: Using the following functions, and some of your own, combine them to make your own creation:

  • askQuestion
  • speak
  • setVoiceName


  • askQuestion('Are you ready?',['yes', 'no', 'maybe'])
    • returns 'yes' when you click on 'yes', etc.
  • speak("Hello")
    • turns text into speech
  • setVoiceName(askQuestion('Which voice?',['English-US Male', 'English-US Female']))
    • asks a question, and then sets the voice name to whatever is selected

Assignment 02

  1. Questions: Answer the questions above, print them out and bring them to class on Monday.
  2. Readings: Read chapter 2 of your text.
  3. On Monday, you will receive your robot; meet in classroom Park 349 first