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
num3 = 3
num4 = 12
 
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")
    
if num3 % 2 == 0:
    print("number", num2, "is even")
else:
    print("number", num2, "is odd")
    
if num4 % 2 == 0:
    print("number", num2, "is even")
else:
    print("number", num2, "is odd")

See an example of functions

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

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:

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 of a 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 funtion values

What's going on here?

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 an example of the function len()
print("Avarage product price is:", my_sum / len(my_list), "euro.")
 
Result:
Avarage product price is 74.99 euro.

Task 1

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.

Do the task See the solution

Task 2

Write a function named is_beatle(), which will take a string as the parameter (for simplicity you can assume that the user will always enter a correct string). The function is to check if the given string is "John Lennon", "Paul McCartney", "Ringo Starr" or "George Harrison". If so, it will return True, otherwise - False.

Do the task See the solution

Next chapter: Write your first game! »