Save
...
12.0 Fundamentals of functional programming
12.1 Functional programming concepts
12.1.4 Pure functions
Save
Share
Learn
Content
Leaderboard
Share
Learn
Cards (27)
What is a key advantage of pure functions in terms of testing?
Easy testability
The 'add(x, y)' function is an example of a
pure function
.
What are pure functions defined as?
Same output, no side effects
A pure function always returns the same output for the same
input
.
Pure functions have no side
effects
What is a key difference between pure and impure functions in terms of output?
Pure functions always consistent
Match the feature with the function type:
Same output for same input ↔️ Pure Function
May have side effects ↔️ Impure Function
What does the example of the pure function 'add(x, y)' return?
Sum of x and y
The 'random_number()' function is an example of an
impure function
.
Why is the 'random_number()' function considered impure?
Returns different output each time
Pure functions do not modify any external
state
Arrange the characteristics of pure functions in a logical order:
1️⃣ Same output for same input
2️⃣ No side effects
3️⃣ Does not modify external state
The 'random.randint(1, 100)' method generates a random
number
Why is the 'random_number()' function impure?
Output varies for same input
What is a pure function?
Same output, no side effects
A pure function can modify external variables or states.
False
A key characteristic of pure functions is that they always return the same
output
What is the primary difference between pure and impure functions?
Side effects
Pure functions always produce the same
result
for the same input.
An example of a pure function is one that returns the sum of two
numbers
What is an example of an impure function?
Random number generation
Pure functions can modify external variables or states.
False
Pure functions are easier to test because they have no
side
effects.
What is one advantage of pure functions in concurrent programming?
Better concurrency
Pure functions may have side effects.
False
Pure functions enhance code clarity by isolating
functionality
.
Why is a random number generator considered an impure function?
Different outputs each time