Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a function in programming?
A function is a reusable block of code that performs a specific task. It helps avoid repeating the same code and makes programs easier to read and maintain.
Click to reveal answer
beginner
Why do we use functions in programming?
Functions help by breaking a program into smaller parts, making it easier to understand, reuse, and test. They save time by avoiding repeated code.
Click to reveal answer
beginner
What is the difference between a function definition and a function call?
A function definition is where you write the code for the function. A function call is when you use or run the function in your program.
Click to reveal answer
intermediate
Explain the concept of input and output in functions.
Functions can take inputs (called parameters) to work with different data. They can also give back results (called return values) after processing.
Click to reveal answer
beginner
How can you think of a function using a real-life analogy?
Think of a function like a coffee machine: you put in coffee beans and water (inputs), press a button (call the function), and get coffee (output). You can use the machine many times without making coffee from scratch each time.
Click to reveal answer
What does a function do in a program?
AStores data permanently
BPerforms a specific task when called
CRepeats the same code multiple times
DDeletes parts of the program
✗ Incorrect
A function performs a specific task when called, helping to organize and reuse code.
Which of these is a function call?
Agreet()
Bfunction greet
Cdef greet():
Dreturn greet
✗ Incorrect
greet() is a function call that runs the code inside the function named greet.
What do we call the values given to a function to work with?
AVariables
BReturns
CLoops
DParameters
✗ Incorrect
Parameters are the inputs given to a function to process.
Why is using functions helpful?
AIt helps reuse code and organize programs
BIt hides errors completely
CIt slows down the program
DIt makes the program longer
✗ Incorrect
Functions help reuse code and organize programs, making them easier to manage.
What is the output of a function?
AThe code inside the function
BThe name of the function
CThe result the function gives back after running
DThe input values
✗ Incorrect
The output is the result the function returns after processing the inputs.
Explain what a function is and why it is useful in programming.
Think about how functions help you write less code and keep things organized.
You got /4 concepts.
Describe the difference between a function definition and a function call with an example.
One is where you write the code, the other is when you use it.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a function in programming?
easy
A. To display output on the screen
B. To reuse a block of code multiple times
C. To store data permanently
D. To create a new variable
Solution
Step 1: Understand what a function does
A function is a reusable block of code designed to perform a specific task.
Step 2: Identify the main purpose
Functions help avoid repeating the same code by allowing reuse whenever needed.
Final Answer:
To reuse a block of code multiple times -> Option B
Quick Check:
Function purpose = reuse code [OK]
Hint: Functions help reuse code blocks easily [OK]
Common Mistakes:
Confusing functions with variables
Thinking functions store data permanently
Believing functions only display output
2. Which of the following is the correct way to define a function named greet that takes no inputs?
easy
A. function greet() {}
B. function greet:
C. def greet()
D. def greet():
Solution
Step 1: Recognize Python function syntax
In Python, functions are defined using the keyword def, followed by the function name and parentheses with parameters (empty if none), ending with a colon.
Step 2: Check each option
def greet(): matches Python syntax. function greet() {} and function greet: use JavaScript style or incorrect syntax. def greet() misses the colon.
Final Answer:
def greet(): -> Option D
Quick Check:
Python function = def name(): [OK]
Hint: Python functions start with def and end with colon [OK]
Common Mistakes:
Omitting the colon at the end
Using JavaScript syntax in Python
Leaving out parentheses
3. What will be the output of this code?
def add(x, y):
return x + y
result = add(3, 4)
print(result)
medium
A. TypeError
B. 34
C. 7
D. None
Solution
Step 1: Understand the function call
The function add takes two inputs x and y and returns their sum.
Step 2: Calculate the return value
Calling add(3, 4) returns 3 + 4 = 7, which is stored in result. Printing result outputs 7.
Final Answer:
7 -> Option C
Quick Check:
3 + 4 = 7 [OK]
Hint: Add numbers inside function returns their sum [OK]
Common Mistakes:
Thinking it concatenates numbers as strings
Expecting a TypeError due to missing return
Assuming print shows None
4. Identify the error in this function definition:
def multiply(a, b)
return a * b
medium
A. Missing colon after function header
B. Incorrect return statement
C. Function name is invalid
D. Parameters should be in square brackets
Solution
Step 1: Check function header syntax
In Python, the function header must end with a colon (:). Here, it is missing after def multiply(a, b).
Step 2: Verify other parts
The return statement and function name are correct. Parameters use parentheses, not square brackets.
Final Answer:
Missing colon after function header -> Option A
Quick Check:
Function header ends with : [OK]
Hint: Function headers always end with a colon in Python [OK]
Common Mistakes:
Forgetting the colon at the end
Using square brackets for parameters
Misnaming the function
5. You want to create a function that returns the square of a number only if the number is positive; otherwise, it returns zero. Which function correctly implements this?
hard
A. def square_if_positive(n):
if n > 0:
return n * n
else:
return 0
B. def square_if_positive(n):
if n >= 0:
return n ** 2
else:
return n
C. def square_if_positive(n):
return n * n if n > 0 else None
D. def square_if_positive(n):
if n < 0:
return n * n
else:
return 0
Solution
Step 1: Understand the condition
The function should return the square of n only if n is positive (greater than 0). Otherwise, it returns 0.
Step 2: Check each option
def square_if_positive(n):
if n > 0:
return n * n
else:
return 0 correctly checks n > 0 and returns n * n, else 0. def square_if_positive(n):
if n >= 0:
return n ** 2
else:
return n includes zero as positive and returns n if negative, which is incorrect. def square_if_positive(n):
return n * n if n > 0 else None returns None instead of 0 when n is not positive. def square_if_positive(n):
if n < 0:
return n * n
else:
return 0 squares negative numbers and returns 0 otherwise, which is opposite.
Final Answer:
def square_if_positive(n):
if n > 0:
return n * n
else:
return 0 -> Option A
Quick Check:
Positive n squared, else zero [OK]
Hint: Check condition n > 0, return square else zero [OK]