# Example 1:
def my_func(num, power=2): # 'num', 'power' - parameters
return num ** power
print(my_func(2, 3)) # 2, 3 - arguments
# Example 2:
print(my_func(2)) # value
var = 12
print(my_func(var)) # variable
print(my_func(var * 2 - 15)) # expression
# Example 3:
def my_func(num):
power = 2 # local variable
return num ** power
print(my_func(5)) # 25
print(power) # NameError: name 'power' is not defined
# variable 'power' is not exist outside my_func
# Example 4:
def my_func(num, power=2):
return num ** power
print(my_func(2, 3)) # 8
print(num) # NameError: name 'num' is not defined
# variable 'num' is not exist outside my_func
# Example 5:
def my_func():
var = 20 # inner variable
return var
var = 10 # outer variable
print(var)
print(my_func())
print(var)
# Output:
# 10
# 20
# 10
# In fact - nothing will happen. Each variable will live it's own life in it's scope.
# Any change of inner variable var will not affect outer variable var and vice versa.
Comments
Leave a comment