 #### Functions

Imagine writing a program in which you define two numeric variables and then check if the variables are even. Finally, you see a message about whether a number is even or not. It looks like this:

``````num1 = 2
num2 = 5

if num1 % 2 == 0:
print("number", num1, "is even")
else:
print("number", num1, "is odd")

if num2 % 2 == 0:
print("number", num2, "is even")
else:
print("number", num2, "is odd")``````

See example: functions

The code written above will work, but it's inefficient, because:

• we write practically the same code twice - we check num1 and num2 separately, conditions and messages are displayed identically,

• it is prone to errors - there can be a typo or a single equals sign in the equation operator (frequent error and difficult to notice).

### WHAT ARE FUNCTIONS FOR?

The best solution would be to write code "on the side" that checks the evenness of a number and use it when needed. Such a solution is possible! This is called a function and is the basis of programming. Instead of writing the same thing several times, we can take the code we want to use repeatedly and convert it into a function:

``````def check_even(num):
if num % 2 == 0:
print("number", num, "is even")
else:
print("number", num, "is odd")``````

#### Declaration of functions

A function starts with a declaration which consists of:

• def keyword,

• mandatory function name (here: check_even). You can name the function any way you like; the rules are the same as for naming variables (see lesson 1).

• then in parentheses you can, if needed, specify the so-called function parameters; a parameter is a variable that you pass to a function and can use inside it

After the declaration, in the code block (remember to indent it) you place the commands, which will be executed after calling this function.

The rest of our program will look the following way:

``````num1 = 2
num2 = 5

check_even(num1)
check_even(num2)``````

See example: function statement

Simple, isn't it?

That's not all!

Sometimes we want the function to calculate something, for example, and the result to be passed on for further processing. For example, we have a list with prices of products in the shopping cart. We want to calculate the sum of these numbers and return it to the programmer, and the programmer will use it further. This is done as follows:

``````def sum_prices(nums):  # nums is a list
result = 0
for element in nums:
result += element
return result``````

### ASSIGNMENT OF FUNCTION VALUES

What's going on here?

• we define a function that we call sum_prices. This function takes the parameter nums. To make things easier, we assume that the programmer will always provide a list of numbers there.

• we declare a result variable, which at the beginning of the function will have the value of 0. We will be adding subsequent numbers from the list to this value.

• we create a loop that will iterate through all the elements of the nums list, and add each subsequent element to the result variable. As a result, after the loop is finished, the value of the result variable will be the sum of all the elements of the nums list.

• The last line of our function is the return result clause. This means that the function is to return the value of this variable to the programmer.

How to use it?

We can now call sum_prices function and assign its result to the variable:

``````my_list = [99.99, 119.99, 49.99, 29.99]

my_sum = sum_prices(my_list)
print(“The purchase price is”, my_sum, “euro.”)

Result:
The purchase price is 299.96 euro.``````

Do you rememberDo you remember how to check the length of the list from the lesson about lists? The len() function is used for this purpose, where we pass the list as a parameter. You can use this function, and then calculate the average price of goods from purchases.

See example: example with the function len()

``````print(“Avarage product price is:”, my_sum / len(my_list), “euro.”)

Result:
Avarage product price is 74.99 euro.``````

Write a function named is_adult(), which takes a number as the parameter (for simplicity you can assume that the user will always enter a correct number). The function is to check if the age of the user is less than 18, in which case it should return False; otherwise - True.

See solution: Solution to Task 1