Pure Functions
A pure function is a function that:
- Always produces the same output for the same input.
- Does not modify external data.
Example:
total = 10000 def roaming_with_friends(amount): return total - amount def eat_outside(amount): return total - amount def spend_for_shopping(amount): return total - amount remaining_amount = spend_for_shopping(1200) print(remaining_amount) # 8800 remaining_amount = eat_outside(300) print(remaining_amount) # 9700 Output: 8800 9700 How does it work?
- Initially, total = 10000.
- Shopping: 10000 - 1200 = 8800.
- Eating outside: 10000 - 300 = 9700.
- The original value of total remains 10000.
In this example, the functions read the global variable but do not modify it. Each calculation starts with the original value.
Impure Functions
An impure function depends on or modifies external data, or produces side effects.
Example:
total = 10000 def roaming_with_friends(amount): return total - amount def eat_outside(amount): return total - amount def spend_for_shopping(amount): return total - amount total = spend_for_shopping(1200) print(total) # 8800 total = eat_outside(300) print(total) # 8500 total = eat_outside(200) print(total) # 8300 Output: 8800 8500 8300 How does it work?
- Initially, total = 10000.
- Shopping: 10000 - 1200 = 8800.
- Eating outside: 8800 - 300 = 8500.
- Eating outside again: 8500 - 200 = 8300.
Here, the global variable total is updated after every calculation. Therefore, the next calculation uses the previously updated value.
This example demonstrates how functions can depend on external state.
Local Variables
A local variable is a variable declared inside a function.
It can be accessed only within that function.
Example:
def eat(amount): total = 500 return total - amount print(eat(100)) Output: 400 Here:
- amount is a parameter and acts as a local variable.
- total is also a local variable.
- Both belong to the function eat().
We cannot directly access these local variables outside the function.
Global Variables
A global variable is declared outside a function.
It can be accessed inside functions in the same module.
Example:
total = 100000 def eat(amount): return total - amount print(eat(500)) Output: 99500 Here, total is a global variable because it is declared outside the function.
The function can read its value without using the global keyword.
Global Keyword in Python
The global keyword is used when we want to assign a new value to a global variable from inside a function.
Example:
total = 10000 def shopping(amount): global total total = total - amount shopping(1200) print(total) Output: 8800 Here:
- total is declared globally.
- The global total statement tells Python that we want to modify the global variable.
- After calling the function, the global value becomes 8800.
Important: We do not need the global keyword just to read a global variable. It is needed when assigning to that global variable inside a function
Local and Global Variables Together
Let's understand which variables are local and which are global.
total = 100 def check(no1): print(total - no1) print(total) check(10) Output: 100 90 Here:
- total is a global variable.
- no1 is a local parameter of the check() function.
- The function can access the global variable total.
- The variable no1 is available only inside check().
If we try:
print(no1)
Python raises a NameError because no1 is not defined outside the function.
Remember: Calling one function inside another function does not change whether a variable is local or global. Its scope depends on where it is defined.
Current Time Example – Impure Function
from datetime import datetime def display_time(): now = datetime.now() t = now.strftime("%H:%M:%S") print("Current Time =", t) display_time() Output: Current Time = 11:45:30 The output changes depending on when we call the function.
Even though there are no arguments, the function depends on the current system time and prints the result.
Therefore, it is an impure function.
