functions
Please Don't Destroy
(our data)
CS 110
Reminders and Announcements
This Week
def name_of_function(parameters):
statement(s)
3
Keyword def marks the start of function header
A function name to uniquely identify it (snake case)
parameters (the way we pass data to a function)
colon (:) marks end of function header
function body has 1 or more statements, which have same indentation level (usually 4 spaces)
A function with inputs
The squareface function Challenge
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
width (int) - width of the face
eye_color (str, optional) - (defaults to blue)
face_color (str, optional) - (defaults to gold)
Output:
A squareface (similar to the one on the right hand of the screen)
4
The squareface function Challenge
Inputs:
center_x (int) - center x-coordinate
center_y (int) - center y-coordinate
width (int) - width of the face
eye_color (str, optional) - (defaults to blue)
face_color (str, optional) - (defaults to gold)
Output:
A squareface (similar to the one on the right hand of the screen)
Eyes should be 1/20 the width of the face; 1/10 the height of the face, offset by ⅕ the width of the face in both directions.
5
Our Function Writing Experience so Far:
So far we have only written functions that are DESTRUCTIVE.
def say_hello(name, time_of_day):
print('Hello there,' + name + '!')
print('How are you this ' + time_of_day + '?')
say_hello(“Connor”, “Morning”)
square(100, 100, 50, color="blue")
square_face(100, 100, 200, eye_color=”blue”, face_color=”green”)
Our Function Writing Experience so Far:
So far we have only written functions that are DESTRUCTIVE.
def say_hello(name, time_of_day):
print('Hello there,' + name + '!')
print('How are you this ' + time_of_day + '?')
make_square(the_canvas, 100, 100, 50, color="blue")
Writing Reporter Functions
Remember, reporters are functions that give us back some value so that we can use it later. While we call reporter functions the same way as all functions, we have to take the extra step of either storing or using the piece of data it gives us back.
result = input("Hello, what is your name? ")
Writing Reporter Functions
Remember, reporters are functions that give us back some value so that we can use it later. While we call reporter functions the same way as all functions, we have to take the extra step of either storing or using the piece of data it gives us back.
result = input("Hello, what is your name? ")
def name_of_function(parameters):
statement(s)
10
Keyword def marks the start of function header
A function name to uniquely identify it (snake case)
parameters (the way we pass data to a function)
colon (:) marks end of function header
function body has 1 or more statements, which have same indentation level (usually 4 spaces)
A function with inputs
def name_of_function(parameters):
statement(s)
return some_variable
11
Keyword def marks the start of function header
A function name to uniquely identify it (snake case)
parameters (the way we pass data to a function)
colon (:) marks end of function header
function body
A reporter function with inputs
"Report" or Return Statement (note, return is NOT a function, it's a keyword like def)
For example…
Here's a function called get_square_area.It has one required input named height. It returns a calculated area to be used later in the program.
def get_square_area(height):
area = height * height
return area
# Invoking the function
result = get_square_area(5)
print(result)
# Say we had 15 of the same square, well then…
print("Area of 15 of those bad bois:", result * 15)
Modules
13
14
What is a Module?
from module_name import function_name
15
Using modules
from random import randint
num_1 = randint(0, 100)
num_2 = randint(0, 100)
num_3 = randint(0, 100)
print(num_1, num_2, num_3)
16
Example: importing a specific function
from random import randint, choice
num_1 = randint(0, 100)
num_2 = randint(0, 100)
num_3 = randint(0, 100)
print(num_1, num_2, num_3)
print(choice("hello?"))
17
Example: Importing more than one thing!
from random import randint, choice
num_1 = randint(0, 100)
num_2 = randint(0, 100)
num_3 = randint(0, 100)
random_colors = [“red”, “green”, “blue”]
one_random_color = choice(random_colors)
18
Example: Importing more than one thing!
Spoilers for Wednesday!
Classes
19
Review: Data Types
3.156
"Go Wildcats!"
15
Review: Data Types
3.156
"Go Wildcats!"
15
float
string
int
Review: Data Types & Variables
magic_pi = 3.156
slogan = "Go Wildcats!"
num_people_awake = 15
A way to define new compound types of data
To define new types of data in programming, we use a Class which…
You can think of a Class as a blueprint that describes the nature of some type of data.
For example…
Say we want to represent a DOG in our programs:
For example…
Say we want to represent a DOG in our programs:
For example…
Say we want to represent a DOG in our programs:
Right now, we don't need to be able to make our own classes
str (the String class) - all methods
"hello".capitalize()
"hello".upper()
"HELLO".lower()
"hello".title()
test_string = "lOl"
test_string.upper()
test_string.swap()
test_string.center(20)
test_string.center(20).strip()
test_string.rjust(20)
txt = " banana "
x = txt.strip()
print("of all fruits", x, "is my fave")