2nd Semester Intro to Computer Science

What you will learn

Reading

Do Now

In your Console

Type the following code

def my_function():
  print("THIS IS MY FUNCTION!")

In Your Notebook

Respond to the following

  1. What does my_function do?
  2. How would you call the function? Practice calling my_function and checking that it does what you expect it to.
  3. 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

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

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_function again
  • 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.

Downloads