Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

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.

Run the following code to see how this is done.
def say_hello(name): """Print 'Hello' followed by name""" print(f'Hello {name}') # Call the function say_hello() twice with different names. say_hello('Harry') say_hello('Hermione')
Hello Harry Hello Hermione

We've changed the definition of the function so that we can pass an argument to it like so:

def say_hello(name):

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

print(f'Hello {name}')

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.

Run the following code to see how this works.
# Assign the string "Bob" to the variable forename. forename = 'Bob' # Pass the value stored in forename to the function say_hello(). say_hello( forename )
Hello Bob

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.

# Loop through a list of names, forename is the iterating variable. for forename in ['Harry', 'Ron', 'Hermione']: # Call say_hello() for each forename. say_hello( forename )
Hello Harry Hello Ron Hello Hermione

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.

def say_hello_to_everyone(list_of_names): """Say hello to everyone in list_of_names""" for name in list_of_names: print(f'Hello {name}') # Call say_hello_to_everyone() passing a list of names as an argument. say_hello_to_everyone( ['Harry', 'Ron', 'Hermione'] )

Functions can be arguments

Run the following code.

The argument passed to print() is the function len() with the argument 'Hello'.

print( len( 'Hello' ) )
5

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.

def say_hello(forename, surname): """Print 'Hello' followed by forename and surname""" print(f'Hello {forename} {surname}') # Call the function say_hello() with two strings. say_hello('Harry', 'Potter') # Call the function say_hello() with two string variables. first_name = 'Dobby' family_name = '' say_hello(first_name, family_name)
Hello Harry Potter Hello Dobby

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.

def say_hello(forename, surname=''): """Print 'Hello' followed by forename and surname. If no surname is given set it to the empty string.""" print(f'Hello {forename} {surname}') # Call the function say_hello() with two strings. say_hello('Harry', 'Potter') # Call the function say_hello() with one string. say_hello('Dobby')
Hello Harry Potter Hello Dobby

Notice in the function definition

def say_hello(forename, surname=''):

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.

Exercise Notebook

Next Notebook