ubuntu2004
Functions II - passing arguments
Passing values (arguments) to functions
The say_hello() function in the last Notebook was a bit pointless. We called it to call the print() function to print "hello". We could have simply written print('Hello') without bothering to define and call our own function say_hello().
Functions become useful when they perform the same task on different values passed to them. For example, say we want to write a function to print "Hello" followed by someone's name, but the name can be different each time we call the function. We can do this by passing a value to the function. The technical name for the value passed to the function is an argument.
This is precisely what we do when we call print() or len(). For example, print('Hello') passes the string argument 'Hello' to the function print() to output to your screen.
Let's change our say_hello() function to take someone's name as an argument.
We've changed the definition of the function so that we can pass an argument to it like so:
name is called a parameter which is a variable in a function definition.
When we call say_hello('Harry') with the argument 'Harry', the string 'Harry' is assigned to the parameter name when the code enters the function. This means when we do
the function outputs Hello Harry.
When we call say_hello('Hermione') with the argument 'Hermione', the string 'Hermione' is assigned to the parameter name so the function outputs Hello Hermione.
Variables can be arguments
In the above example we passed a string to say_hello(). But we can also pass a string variable to it.
This allows us to do more complicated things like looping through a list of names and calling say_hello() for each one. An example is shown in the following code.
Also note that the argument passed to the function (e.g., forename) does not need to have the same name as the function's parameter (e.g., name).
Lists and dictionaries can be arguments
As well as passing strings and numbers to a function, lists and dictionaries can also be passed.
The function say_hello_to_everyone() has a list as a parameter. It says "Hello" to everyone in the list by looping through it.
Functions can be arguments
The argument passed to print() is the function len() with the argument 'Hello'.
Functions can take more than one argument
The say_hello() function has one parameter. But functions can have any number of parameters.
Let's rewrite say_hello() to take two arguments: a first name and a surname.
In the first call, forename is assigned the value 'Harry' and surname the value 'Potter'.
In the second call, forename is assigned the value 'Dobby' and surname is assigned an empty string.
Parameters can have default values
In the last example Dobby didn't have a surname, so we had to set family_name to an empty string so that it would print correctly. Python, however, lets us assign a default value to a function's parameter. When the function is called without passing an argument to that parameter, the parameter takes on the default value.
Let's rewrite say_hello() so that if the surname is not passed it is assigned the empty string.
Notice in the function definition
the parameter surname is assigned the empty string if a surname argument is not passed to the function. So when we call say_hello('Dobby') forename is assigned the string 'Dobby' and surname is assigned the empty string.