Function Parameter Variables in Python
Let us have the following function:
def func(num):
return num ** 2
Let's call the function, passing the number 2 as a parameter:
print(func(2))
However, it is not necessary to pass a number - you can also use a variable containing the value we need:
tst = 3
print(func(tst)) # 9
Three variables with numbers are given:
tst1 = 2
tst2 = 4
tst3 = 6
Make a function that will take three numbers as parameters and find their sum. Display the sum of the variables specified above.
Given a function func and a variable tst:
def func(lst):
sum = 0
for el in lst:
sum += el
return sum
tst = [1, 3, 6]
Use the function to find the sum of the elements of the variable tst.