Function Definitions

CS 65: Introduction to Computer Science I

Recall: Abstraction is a big idea in computer science

You don't have to know how to farm to be a chef - they work at different levels of abstraction in food production.

In computer science, you build bigger, more complex things by using the building blocks others (or you) have provided.

We've seen a lot of function calls so far

In [ ]:
print("Hello world!")
type(2.70)
fahrenheit_temp =  input("Enter the temperature in Fahrenheit ") )
len(name)
user_input = int( input("Enter a number greater than "+str(target_number)+" ") )
max(rainfall_amounts)
with open("gettysburg.txt") as gettysburg_file:
    gettysburg_text = gettysburg_file.readlines()

Why are these awesome?

  • We don't have to think about the code that makes these work in order to use them (abstraction)
  • The code these programmers wrote is reusable - the same code they wrote once gets used in many many programs by many different programmers

Defining your own functions

The syntax for defining your own function includes the following

  • keyword def (short for define)
  • a name/identifier that you pick (same rules as variable names)
  • parentheses ( ) (later, we'll put things inside these parentheses)
  • colon :
  • indented block of code
In [1]:
import random

def magic_8_ball():
    rand_val = random.randint(1,3)
    
    if rand_val == 1:
        print("It is Certain.")
    elif rand_val == 2:
        print("Don't count on it.")
    else:
        print("Reply hazy, try again.")

This function won't run or do anything until you call it. Call it just like any other function you've used.

In [4]:
magic_8_ball()
Reply hazy, try again.

Group Exercise

Put just the magic_8_ball() function definition in a file and run it.

Notice that nothing happens. Discuss: why it does that?

Then, add a function call to your program and run it again. Discuss: what happens now, and why does it do that?

You can also now call the function interactively from the interactive shell. Try this and discuss the results.

When you test a single function in this way, you are performing a kind of unit test - this would be an important step in including the function in a larger program.

Calling your function as part of a larger program

Now you can use the function you created in something bigger. This example presents the menu for the user to use the Magic 8 Ball, but we don't have to worry about how it works.

In [5]:
user_response = input("Do you have a question in your mind for the Magic 8 Ball? (yes/no) ")

while user_response == "yes":
    print("The Magic 8 Ball says....")
    magic_8_ball()
    user_response = input("Do you have another question in your mind for the Magic 8 Ball? (yes/no) ") 
Do you have a question in your mind for the Magic 8 Ball? (yes/no) yes
The Magic 8 Ball says....
It is Certain.
Do you have another question in your mind for the Magic 8 Ball? (yes/no) yes
The Magic 8 Ball says....
Don't count on it.
Do you have another question in your mind for the Magic 8 Ball? (yes/no) yes
The Magic 8 Ball says....
It is Certain.
Do you have another question in your mind for the Magic 8 Ball? (yes/no) no

Here's what the entire file looks like:

In [ ]:
import random

def magic_8_ball():
    rand_val = random.randint(1,3)
    
    if rand_val == 1:
        print("It is Certain.")
    elif rand_val == 2:
        print("Don't count on it.")
    else:
        print("Reply hazy, try again.")
        
        
        
user_response = input("Do you have a question in your mind for the Magic 8 Ball? (yes/no) ")

while user_response == "yes":
    print("The Magic 8 Ball says....")
    magic_8_ball()
    user_response = input("Do you have another question in your mind for the Magic 8 Ball? (yes/no) ")

Why should we write our own functions?

  • Easier problem-solving - break big tasks into small ones
  • Better organization - easier to find/change the code you're looking for
  • Easier testing/debugging - you can test each function by itself before integrating into the bigger program
    • Testing an individual function by itself is called unit testing
  • Reusable - you can write code that will be useful many times in this program and possibly later ones

What might our Magic 8 Ball code look like inside a bigger program?

When you call a function, Python will remember where you left off and continue executing there when the function is complete.

In [6]:
import random

def magic_8_ball():
    rand_val = random.randint(1,3)
    
    if rand_val == 1:
        print("It is Certain.")
    elif rand_val == 2:
        print("Don't count on it.")
    else:
        print("Reply hazy, try again.")
       
    
        
def magic_8_ball_dialog():
    user_response = input("Do you have a question in your mind for the Magic 8 Ball? (yes/no) ")

    while user_response == "yes":
        print("The Magic 8 Ball says....")
        magic_8_ball()
        user_response = input("Do you have another question in your mind for the Magic 8 Ball? (yes/no) ")

        
        
def jack_in_the_box():
    wait_time = random.randint(1,100)
    counter = 0
    while counter < wait_time:
        print(".")
        counter += 1
    print("POP!")
    
    
    
def rubiks_cube():
    #pretend there is some awesome Rubik's cube code here
    print()
    
    
    
def main():
    
    toy_number = 99
    
    while toy_number != 0:
        print("Available toys:")
        print("1: Magic 8 Ball")
        print("2: Jack-in-the-Box")
        print("3: Rubik's Cube")
        print("0: Quit")
        toy_number = int(input("Which toy do you want to play with? "))
        
        if toy_number == 1:
            magic_8_ball_dialog()
        elif toy_number == 2:
            jack_in_the_box()
        elif toy_number == 3:
            rubiks_cube()
            
    print("Have a nice day!")
        
main()
        
    
Available toys:
1: Magic 8 Ball
2: Jack-in-the-Box
3: Rubik's Cube
0: Quit
Which toy do you want to play with? 2
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
POP!
Available toys:
1: Magic 8 Ball
2: Jack-in-the-Box
3: Rubik's Cube
0: Quit
Which toy do you want to play with? 1
Do you have a question in your mind for the Magic 8 Ball? (yes/no) yes
The Magic 8 Ball says....
Reply hazy, try again.
Do you have another question in your mind for the Magic 8 Ball? (yes/no) yes
The Magic 8 Ball says....
It is Certain.
Do you have another question in your mind for the Magic 8 Ball? (yes/no) no
Available toys:
1: Magic 8 Ball
2: Jack-in-the-Box
3: Rubik's Cube
0: Quit
Which toy do you want to play with? 0
Have a nice day!