Function is a dedicated block of code, which only runs when it is called.
It is also an essential element of the DRY (Don’t Repeat Yourself) methodology, i.e.
where a functionality will be used many times, we can use the function.
In Python a function is defined using the def keyword:
1 example – def simple ops
def adding(a,b):
return a+b
print(adding(1,2))
print(adding(2,3))
print(adding(-1,2))

def add_numbers(a:int,b:float):
return a+round(b,2)
print(add_numbers(1,2)) #3
print(add_numbers(3,4.8)) #7.8
print(add_numbers(9,0.33)) #9.33
print(add_numbers(2,math.pi)) #5.1419999999999995
def add_numbers(a:int,b:float):
return round(a+b,3)
print(add_numbers(2,math.pi)) #5.142
2 example – if statement included
def expon_positiv (x,y):
if x < 0:
return '---'
elif x > 0:
return x**y
print(expon_positiv(0.1,2))
print(expon_positiv(2,2))
print(expon_positiv(-2,2))
print(expon_positiv(2,-2))

def even(a:int):
if a % 2 == 0:
return a
else:
return a-1
print(even(1))
print(even(2))
print(even(7))
print(even(8))

3 example – with global param
acum_base = 0
def accumulate(x):
global acum_base
acum_base +=x
return acum_base
print(accumulate(3))
print(accumulate(2))
print(accumulate(7))

4 example – *args, *kwargs
*args collects any number of positional arguments and packs them into a tuple.
**kwargs collects named arguments (keyword arguments) into a dictionary (dict).
def function1(*args):
return sum(args)
print(function1(1,4,5))
# *args + loop
def function1(*args):
total = 0
for i in args:
total += i
return total
print(function1(1,2,3))

#**kwargs
def function2(**names):
print("My name is " + names["fname"])
print("My last name is " + names["lname"])
print("My full name: " + names["lname"], names["fname"])
function2(fname = "Agnieszka", lname = "Szczep")

5 example – def const value then change it
def function3(music = "Rock"):
print("I love " + music + " music")
function3()
function3("Hip-hop")
function3("Trance")

7 example – math recursion model
def function4(k):
if(k > 0):
result = k + function4(k - 1)
print(result)
else:
result = 0
return result
print("Recursion Example Results")
function4(4)

8 example mutability revisited
numbers = [[0,1,2],[3,4,5],[6,7,8]]
def numbers_board(your_number = 0, row = 0, column = 0):
print(" A B C")
numbers[row][column] = your_number
for count, row in enumerate(numbers):
print(count,row)
print(numbers)
numbers_board(your_number=9, row=2, column=1)
print(numbers)
''' ouput1 numbers:
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
output2 numbers_board:
A B C
0 [0, 1, 2]
1 [3, 4, 5]
2 [6, 9, 8]
output3 numbers:
[[0, 1, 2], [3, 4, 5], [6, 9, 8]]'''
value = "set a number"
print(id(value)) #output: 1909035942960
def numbers():
global value
value = "0-9"
print(id(value)) #output: 1909035390448
print(value) #output: 0-9
numbers()
print(value) #output: 0-9
print(id(value)) #output: 1909035390448
def numbers_board(numbers, your_number = 0, row = 0, column = 0, just_show = False):
print(" A B C")
if not just_show:
numbers[row][column] = your_number
for count, row in enumerate(numbers):
print(count,row)
return numbers
numbers_board(numbers, your_number=9, row=2, column=1)






