

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
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
If you take nothing else away from this course, please take this: pure functions are fantastic. They have two properties:
In short: pure functions don't do anything with anything that exists outside of their scope.
Click to play video
def find_max(nums: list[int]) -> float:
max_val: float = float("-inf")
for num in nums:
if max_val < num:
max_val = num
return max_val
# instead of returning a value
# this function modifies a global variable
global_max: float = float("-inf")
def find_max(nums: list[int]) -> None:
global global_max
for num in nums:
if global_max < num:
global_max = num
There's a bug in the convert_file_format function! Right now, it relies on data outside its own scope. These global values can be changed by other parts of the code, so they are not guaranteed to be the same every time convert_file_format is called.
Fix the bug by making convert_file_format a pure function. It should depend only on data that is scoped inside the function.
Don't change the signature of convert_file_format.