A pure function is a function that always returns the same result given the same inputs and has no side effects (does not change anything outside itself).
Click to reveal answer
beginner
Why are pure functions important in programming?
Pure functions are important because they are predictable, easier to test, and help avoid bugs caused by changing external data.
Click to reveal answer
intermediate
Which of these is a side effect that a pure function must NOT have?
Changing a global variable, modifying input arguments, or performing input/output like printing to the screen are side effects pure functions must avoid.
Click to reveal answer
beginner
Example: Is this Ruby method pure?
def add(a, b)
a + b
end
Yes, this method is pure because it always returns the sum of a and b without changing anything else.
Click to reveal answer
intermediate
Example: Is this Ruby method pure?
def add_and_print(a, b)
sum = a + b
puts sum
sum
end
No, this method is not pure because it prints to the screen, which is a side effect.
Click to reveal answer
What does a pure function always guarantee?
ADepends on external data
BSame output for same inputs and no side effects
CPrints output to the screen
DChanges global variables
✗ Incorrect
Pure functions always return the same result for the same inputs and do not cause side effects.
Which of these is NOT a characteristic of a pure function?
ANo side effects
BReturns a value
CModifies input arguments
DDeterministic output
✗ Incorrect
Pure functions do not modify their input arguments.
In Ruby, which method below is pure?
Adef multiply(x, y); x * y; end
Bdef greet(name); puts "Hello, #{name}"; end
Cdef save_to_file(data); File.write('file.txt', data); end
Ddef increment_counter; $counter += 1; end
✗ Incorrect
Only the multiply method is pure because it returns a value without side effects.
Why is it easier to test pure functions?
ABecause they depend on external state
BBecause they print output
CBecause they modify global variables
DBecause they always produce the same output for the same input
✗ Incorrect
Pure functions are easier to test since their output depends only on their input.
Which of these is a side effect that breaks purity?
AChanging a global variable
BReturning a calculated value
CUsing local variables
DCalling another pure function
✗ Incorrect
Changing a global variable is a side effect and breaks the purity of a function.
Explain what a pure function is and why it is useful.
Think about functions that behave like a math formula.
You got /4 concepts.
Give an example of a pure function in Ruby and explain why it is pure.
Use a simple method like adding two numbers.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes a pure function in Ruby?
easy
A. A function that reads user input and prints output directly.
B. A function that always returns the same output for the same input and has no side effects.
C. A function that modifies global variables to keep track of state.
D. A function that changes the value of its arguments.
Solution
Step 1: Understand pure function definition
A pure function always returns the same result when given the same input and does not affect anything outside itself.
Step 2: Compare options to definition
A function that always returns the same output for the same input and has no side effects. matches this definition exactly. Options A, B, and D involve side effects or changing external state.
Final Answer:
A function that always returns the same output for the same input and has no side effects. -> Option B
Quick Check:
Pure function = consistent output + no side effects [OK]
Hint: Pure functions never change outside data and always return same output [OK]
Common Mistakes:
Thinking functions that modify variables are pure
Confusing input/output consistency with side effects
Assuming printing output is allowed in pure functions
2. Which Ruby function below is a pure function?
easy
A. def add(a, b); a + b; end
B. def add(a, b); puts a + b; end
C. def add(a, b); @sum = a + b; end
D. def add(a, b); print a + b; end
Solution
Step 1: Check each function for side effects and output consistency
def add(a, b); a + b; end returns sum without changing anything outside or printing. def add(a, b); puts a + b; end prints (side effect). def add(a, b); @sum = a + b; end changes instance variable (side effect). def add(a, b); print a + b; end prints (side effect).
Step 2: Identify pure function
Only def add(a, b); a + b; end meets pure function rules: same output for same input, no side effects.
Final Answer:
def add(a, b); a + b; end -> Option A
Quick Check:
Pure function = no side effects + returns value [OK]
Hint: Pure functions return values without printing or changing variables [OK]
Common Mistakes:
Confusing printing output with returning value
Assuming modifying instance variables is allowed
Assuming print doesn't cause side effects
3. What is the output of this Ruby code?
def multiply(x, y)
x * y
end
result = multiply(3, 4)
puts result
medium
A. Error
B. nil
C. 12
D. 7
Solution
Step 1: Understand the multiply function
The function returns the product of x and y, so multiply(3, 4) returns 12.
Step 2: Check the output statement
puts prints the returned value, so it prints 12.
Final Answer:
12 -> Option C
Quick Check:
multiply(3,4) = 12 [OK]
Hint: Pure functions return predictable output; multiply(3,4) = 12 [OK]
Common Mistakes:
Thinking function returns nil if no explicit return
Confusing addition with multiplication
Expecting error due to missing return keyword
4. Identify the error in this Ruby function that is supposed to be pure:
def increment(n)
n += 1
puts n
end
medium
A. It modifies the input variable n.
B. It uses invalid syntax for increment.
C. It does not return any value explicitly.
D. It prints output, causing a side effect.
Solution
Step 1: Check for side effects
The function prints the value of n, which is a side effect because it affects outside world.
Step 2: Verify other aspects
Modifying n locally is allowed; syntax is correct; implicit return is allowed but printing breaks purity.
Final Answer:
It prints output, causing a side effect. -> Option D
Quick Check:
Pure functions must not print or cause side effects [OK]
Hint: Printing inside function breaks purity due to side effects [OK]
Common Mistakes:
Thinking local variable change breaks purity
Assuming implicit return is an error
Ignoring side effects caused by printing
5. You want to write a pure function in Ruby that takes a list of numbers and returns a new list with each number doubled. Which implementation is pure?
hard
A. def double_list(nums)
nums.map { |n| n * 2 }
end
B. def double_list(nums)
nums.collect! { |n| n *= 2 }
nums
end
C. def double_list(nums)
nums.map! { |n| n * 2 }
end
D. def double_list(nums)
nums.clear
nums.push(2, 4, 6)
end
Solution
Step 1: Understand pure function requirements
A pure function returns new data without changing input or external state.
Step 2: Analyze each option
def double_list(nums)
nums.collect! { |n| n *= 2 }
nums
end uses collect! which modifies original array (side effect). def double_list(nums)
nums.map { |n| n * 2 }
end returns a new array with doubled values without changing original. def double_list(nums)
nums.map! { |n| n * 2 }
end uses map! which modifies original array (side effect). def double_list(nums)
nums.clear
nums.push(2, 4, 6)
end clears and modifies original array (side effect).
Final Answer:
def double_list(nums)
nums.map { |n| n * 2 }
end -> Option A
Quick Check:
Pure function = no input change + returns new data [OK]
Hint: Use non-destructive methods like map, avoid modifying input [OK]
Common Mistakes:
Using destructive methods like map! or clear
Modifying input array instead of returning new one
Confusing each with map for transformation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.