Below is an example of a function definition in Python.

It describes a piece of reusable code which can be called whenever we want to use it.

 Click each part of the code below to learn how it works.

def add_two_numbers(first_num, second_num):
answer = first_num + second_num
return answer

Definition

Click on part of the code above or below to see a definition here.

Function keyword

In Python, the def keyword tells the computer that a function is about to be defined.

As a good practice, functions are commonly placed at the top of the program.

Function name

The name of a function can be anything. In our example it is add_two_numbers.

The naming rules are the same as for variables; it must start with a letter and have no spaces. Best practice is to create a meaningful name.

When you want to use the function, you 'call' it. For example: add_two_numbers()

Parameters

A parameter is like a variable, so you use the same naming conventions. Give parameters meaningful names that reflect what they do.

We can think of parameters as placeholders. When we call a function, we specify arguments which provide the values to replace those placeholders.

A parameter is used in the body of the function (the indented statements below the first line). We say, 'Parameters are passed to the function'.

Local variable

This statement starts with declaring a local variable.

'Local' means that the variable can be used anywhere within the function but is not available for use outside of it, in the rest of the program.

Programming statement

A function can contain many programming statements, which are instructions to be carried out when the function is called.

As a whole, this statement carries out a calculation and stores the result in the local variable answer.

Return statement

A return statement is a value (int, string, list, etc.) that is the result of the function.

A function may or may not have a return statement. A function that does not have a return statement is called a procedure.

Function name

We begin our call with the name of the function we want to call.

We do not use the def keyword when calling a function.

Arguments

Arguments are values that we pass to a function when it is called.

When the function is invoked, the value of each argument is assigned to the corresponding parameter in the function's code.

When we call a function, we pass one argument for each parameter it requires.

 Click each part of the code below to learn how it works.

add_two_numbers(4, 16)

This function will add two arguments and return the result.

def add_two_numbers(first_num, second_num):
answer = first_num + second_num
return answer

Drag two arguments to the parameters to see the return.

Arguments

4
12
19
22
29
36
add_two_numbers(
, 
)

Output

Notice that the local variable is defined and the calculation is performed in one line of code. This is followed by a separate return statement.

This function will concatenate two arguments and return the result.

def animal_mash_up(first_animal, second_animal):
return first_animal + second_animal

Drag two arguments to the parameters to see the return.

Arguments

Bat
Cat
Dog
Fish
Bear
Ant
animal_mash_up(
, 
)

Output

Notice that all we have is the return statement followed by the calculation. Just one line!

This function will check both arguments to determine whether they meet the criteria.

def ride_pass(age, height):
if age > 8 and height > 120:
check = True
else:
check = False
return check

Drag two arguments to the parameters to see the return.

Arguments

1
7
12
112
128
154
ride_pass(
, 
)

Output

Here the variable, 'check', is of a Boolean type (true/false). Notice that order of the argument placement matters. Try finding a combination of arguments which produce a different result when swapped.

You may now return to the course.