Python Function Values

You can hand values over to your functions for processing. To hand a value over to your function, type variable names between the round brackets of the function. Let's see how this works.

Set up a new function for this. Press the enter key on your keyboard just after you first function. Press the enter key two times. Now add this as the function definition:

def addition(first_number):

As the code for the function, add this line (don't forget to indent it):

print("First Number to add is:", first_number)

Your code should look like this:

Basic Python function with one parameter

Notice that we have two lines of space separating the functions, and two lines of space separating the functions from the line rest of the code. This is PyCharm's recommended way of doing things.

To call the second function into action, add a calling line:

# error_message()


Here's what your code should look like. Notice that we've commented out the error_message line. You can do the same:

Calling a Python function with a parameter

Run your code and try it out. You should see this in the output window:

First Number to add is: 3

The number 3 between the round brackets of addition gets handed over to the first_number variable between the round brackets of our addition function. The value inside of first_number is then used in a print statement.

In our code above, we're hard-coding a value of 3 between the round brackets of addition. This number can come from a variable instead:

number_one = 3

It can get a bit complicated when you're handing variable names over to functions, so for now we'll stick with hard-coded values.

You can hand more than one piece of data over to your functions. In your function declaration, add second variable name between the round brackets. You separate each variable name with a comma. (Incidentally, the variable names inside of a function declaration are called parameters.)

Add a second parameter to your function:

def addition(first_number, second_number):

Now add another print line:

print("Second Number to add is:", second_number)

Your code should look like this:

Calling a function with two parameters

Try to run your code and you should see an error message appear:

TypeError: addition() missing 1 required positional argument: 'second_number'

The reason for the error is the line that calls the addition function into action:


The function we defined now has two parameters, first_number and second_number. Our two parameters are not optional. You need to include values for both when you're calling the function into action. Change your calling line to this:

addition(3, 5)

We now have values for both parameters. Our two values are 3 and 5. The values are separated by a comma. (Incidentally, when you're handing values to a function, you are said to be passing arguments to a function. You pass an argument to a parameter.)

Now that you have two arguments, run your code again. You should see that it runs OK now, and this appears in the output window:

First Number to add is: 3
Second Number to add is: 5

Now let's actually add those numbers, instead of just printing them out:

Set up a new variable in your function called answer. Then add the first and second numbers:

answer = first_number + second_number

Delete one of the print lines and amend the other:

print("The answer to the addition is:", answer)

Your code will then look like this:

A Python function with two parameters

Run your code and you should see this in the output window:

The answer is to the addition is 8

In the next lesson, you'll learn about return values for Python functions.

Function Return Values >