

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Pure Functions
incomplete
2: Pure Function Review
incomplete
3: Reference vs. Value
incomplete
4: Pass by Reference Impurity
incomplete
5: Input and Output
incomplete
6: Should I I/O?
incomplete
7: No-Op
incomplete
8: Memoization
incomplete
9: Referential Transparency
incomplete
10: Pure Functions Practice
incomplete
11: Pure Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Pure functions have a lot of benefits. Whenever possible, good developers try to use pure functions instead of impure functions. Remember, pure functions:
Return the same result if given the same arguments. They are deterministic.
Do not change the external state of the program. For example, they do not change any variables outside of their scope.
Do not perform any I/O operations (like reading from disk, accessing the internet, or writing to the console).
These properties make pure functions easier to test, debug, and think about.
Refer to the following examples to answer the questions.
def multiply_by2(nums: list[int]) -> list[int]:
products: list[int] = []
for num in nums:
products.append(num * 2)
return productsbalance: int = 1000
cars: list[str] = []
def buy_car(new_car: str) -> None:
global balance
cars.append(new_car)
balance -= 69import random
def roll_die(num_sides: int) -> int:
return random.randint(1, num_sides)