In my previous article we were talking about the basics of functions. If you haven’t read it yet, feel free to do so. Today we’ll be talking about functions with parameters.
Parameters vs Arguments
You already know that we use function names followed by parentheses to call functions. Now, what are the parentheses actually for? In the parentheses we usually pass some data that is then used in the function body. It can be a variable or a literal. In the function definition we call the data parameters. There may be any number of parameters, including zero like in our example in the previous article. When we call the function in the code, we call the data passed to the function arguments.
Let’s have a look at the following example with just one parameter:
def inch_to_cm(inches):
print("{} inches = {} centimeters".format(inches, inches * 2.54))
lengths = [4, 12.004, 7.1, 0.003, 5.5]
for length in lengths:
inch_to_cm(length)
The parameter’s name is inches. The function converts inches to centimeters. It prints the converted value to the output. Below the function definition we have some code where the function is called in the for loop. We pass the loop variable as an argument to the function: inch_to_cm(length). Here length is the argument which corresponds to the parameter inches. This means it’s used in the function exactly the same way as the parameter was used in the definition. The for loop iterates over all the elements of the lengths list, a total of 5 iterations. So, the function is called 5 times, once for each list element passed as an argument. Here’s the output:
4 inches = 10.16 centimeters
12.004 inches = 30.49016 centimeters
7.1 inches = 18.034 centimeters
0.003 inches = 0.00762 centimeters
5.5 inches = 13.97 centimeters
Types of Parameters
In Python we DON’T declare the types of parameters. That means that when we define a function, we actually define a whole family of functions which can work in a different way depending what type the parameters are. Here’s a simple example. Let’s define a function that takes two parameters and adds them together:
>>> def handle_values(value1, value2):
... return value1 + value2
...
Now we can use integer arguments and it’ll work:
>>> handle_values(3, 8)
11
But we can also pass floats to the function:
>>> handle_values(1.17, 3.23)
4.4
Now, if we pass strings, the function will work as well, because we can use the + operator with strings. But with strings the + operator has a different meaning: instead of adding, it concatenates the strings:
>>> handle_values("Oh ", "no")
'Oh no'
It can be also used with lists:
>>> handle_values([1, 2, 3], [4, 5, 6, 7])
[1, 2, 3, 4, 5, 6, 7]
So, as you can see, the function can operate on quite a lot of types of parameters. Actually it can work on any parameters for which the + operator makes sense. If we use parameters with which the operator makes no sense, we’ll get an error, like for example if we try to concatenate a list and an int:
>>> handle_values([1, 2, 3], 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in handle_values
TypeError: can only concatenate list (not "int") to list
We say Python functions are polymorphic. A polymorphic function is a function that can work with arguments of different types.