What you will learn
- Define and identify: abstraction, def
- Create functions
Reading
- Associated Reading - section 3.2 of Book
- Associated Readings 3.2
Do Now
In your Console
Type the following code
def my_function():
print("THIS IS MY FUNCTION!")
In Your Notebook
Respond to the following
- What does
my_functiondo? - How would you call the function? Practice calling
my_functionand checking that it does what you expect it to. - How would you add arguments to
my_function?
Lab
Lab Exercise 1
Create a function, birthday_song, that prints out the happy birthday song to whatever name is input as an argument. The contract should be:
# Name: birthday_song
# Purpose: birthday_song prints out a personalized birthday song
# Arguments: name, string
# Returns: none
def birthday_song(name):
#your code goes here
Lab Exercise 2
- Create a function that randomly picks 5 cards from a deck
- The cards can repeat
Write out the contract for this function using the list.
number = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
Bonus
- Practice passing in lists as an argument to a function.
- What is different about passing in a list as an argument?
- Read about list aliasing in section 3.4 of the associated reading, and write down what is happening in this case.
Check your understanding
Pick an answer for each question. You can try again until you get it right.
1. What does the def keyword do?
- Runs a function immediately
- Deletes a function
- Prints a message
- Defines a function without running its body yet
def creates the function; its body only runs when the function is called.
2. After defining def my_function():, how do you make its body run?
- Type
my_function - Call it with
my_function() - Type
def my_functionagain - Import it
Parentheses after the name call the function.
3. What is abstraction in programming?
- Using a name for a set of steps so you can use it without thinking about the details
- Writing code with no comments
- Repeating the same code many times
- Making a variable global
A function hides the details behind a name, so you can reuse it and think at a higher level.