0
0
Intro to Computingfundamentals~20 mins

Functions (reusable code blocks) in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
trace
intermediate
2:00remaining
Trace the output of a function call

What is the output of the following code when greet('Anna') is called?

Intro to Computing
def greet(name):
    message = f"Hello, {name}!"
    return message

print(greet('Anna'))
AHello, Anna!
BError: name not defined
Cgreet('Anna')
DHello, name!
Attempts:
2 left
💡 Hint

Look at how the function uses the input name inside the message.

🧠 Conceptual
intermediate
2:00remaining
Understanding function parameters and arguments

Which statement best describes the difference between a function parameter and an argument?

AA parameter is a variable in the function definition; an argument is the actual value passed when calling the function.
BA parameter is the value passed to a function; an argument is the function's output.
CA parameter and an argument are the same thing and can be used interchangeably.
DA parameter is the function's output; an argument is the function's name.
Attempts:
2 left
💡 Hint

Think about the function definition versus the function call.

Comparison
advanced
2:00remaining
Compare two function definitions

Which function definition correctly calculates the square of a number and returns it?

Intro to Computing
Option A:
def square(x):
    return x * x

Option B:
def square(x):
    print(x * x)

Option C:
def square(x):
    return x + x

Option D:
def square(x):
    return x ** 3
AOption B
BOption D
COption A
DOption C
Attempts:
2 left
💡 Hint

Remember that the square of a number is the number multiplied by itself.

identification
advanced
2:00remaining
Identify the error in the function

What error will occur when running this code?

Intro to Computing
def add_numbers(a, b):
    return a + b

result = add_numbers(5)
print(result)
ASyntaxError: invalid syntax
BTypeError: missing 1 required positional argument: 'b'
CNameError: name 'result' is not defined
DNo error, output is 5
Attempts:
2 left
💡 Hint

Check how many arguments the function expects versus how many are given.

🚀 Application
expert
2:00remaining
Predict the output of nested function calls

What is the output of this code?

Intro to Computing
def multiply(x, y):
    return x * y

def add_and_multiply(a, b, c):
    return multiply(a + b, c)

result = add_and_multiply(2, 3, 4)
print(result)
A24
B14
CError: multiply function not defined
D20
Attempts:
2 left
💡 Hint

First add a and b, then multiply the sum by c.