APCSP Unit 5 CodeHS Flashcards
What is the output of the following code?
def quadruple_number(x):
quad_x = 4 * x
print(quad_x)
x = 5
quadruple_number(x)
20
Why do we write functions?
Make our code easier to understand by giving a readable name to a group of instructions
Avoid writing repeated code
Make our code reusable
What is the parameter of the function quadruple_number?
def quadruple_number(x):
quad_x = 4 * x
print(quad_x)
x
What is printed by the following code?
def print_numbers(first, second, third):
print(first)
print(second)
print(third)
middle = 5
print_numbers(4, middle, 6)
4
5
6
What are the parameters of the print_numbers function?
def print_numbers(first, second, third):
print(first)
print(second)
print(third)
first, second, third
If we want to draw a circle using our helpful draw_circle function at position (300, 400) with a radius of 40 and color blue, which is the correct function call?
As a reminder, here’s the function header for draw_circle:
def draw_circle(radius, color, x, y)
draw_circle(40, Color.blue, 300, 400)
Do functions need to have parameters?
No, they don’t have to
Which of the following functions successfully returns double the variable ‘number’?
def my_function(number):
return number*2
def my_function(number*2):
return
def my_function():
return number
def my_function(number):
number*2
def my_function(number):
return number*2
Which of the following functions successfully returns the number 10?
def my_function():
10
def my_function(10):
return
def my_function():
return 10
def my_function():
print 10
def my_function():
return 10
How many parameters go into the function sum?
def sum(first, second):
result = first + second
return result
2
How many return values come out of the function sum?
def sum(first, second):
result = first + second
return result
1
In which namespace is the variable ‘x’ defined in the following program?
x = 10
def add_nums():
y = 2
z = x + y
print z
Global namespace
If added to the program below, which of the following additional functions would not cause an error?
x = 10
def add_nums():
y = 2
z = x + y
print z
_____________________
def subtract_nums():
z = x - y
print z
def subtract_nums():
z = z - x
print z
def subtract_nums():
y = 5
z = x - y
print z
def subtract_nums():
z = y
z = x - y
print
def subtract_nums():
y=5
z=x-y
print z
Which keywords are relevant to exceptions in Python?
try and except
What type of error occurs when a programing is expecting an integer value and the user enters a string?
ValueError