5.2. Arguments#
We can pass information to a function in the form of arguments. When a function is declared, the input arguments are listed inside the brackets and separated by commas.
def function(argument):
commands to be executed
To demonstrate this, enter the following code into your program.
# Arguments
def greet(name):
print(f"Hello {name}")
print()
greet("Joel")
Here we have declared a function called greet()
that has a single input argument name
. We then call this function specifying the input string "Joel"
. Run your program and you should see the following added to the console.
Hello Joel
Lets say that Ellie has entered the chat and we would like to greet them. To demonstrate this, enter the following code into your program.
greet("Ellie")
Rerun your program and you should see the following added to the console output.
Hello Ellie
5.2.1. Multiple arguments#
A Python function can have any number of arguments which are listed in the brackets separated by commas.
def function(argument1, argument2, ...):
commands to be executed
To demonstrate this, enter the following code into your program.
# Multiple arguments
def print_name_and_age(first_name, last_name, age):
print(f"Name: {first_name} {last_name}")
print(f"Age : {age}")
print()
print_name_and_age("Joel", "Miller", 56)
Run your program and you should see the following added to the console.
5.2.1 Multiple arguments
------------------------
Name: Joel Miller
Age : 56
5.2.2. Arbitrary arguments *args#
If you don’t know how many arguments will be passed into your function we can use arbitrary arguments by adding an *
before the argument name. This means that the arbitrary argument will be a tuple (an ordered set of objects).
def function(*args)
commands to be executed
Note
The argument name *args
is not a requirement and we could use any argument name, e.g., *names
. However, it is common to use *args
as it reminds someone reading the code that the arbitrary argument is a tuple.
To demonstrate this, enter the following code into your program.
# Arbitrary arguments
def print_names(*args):
for name in args:
print(f"{name}")
print()
print_name("Ellie", "Joel")
Run your program and you should see the following added to the console.
Ellie
Joel
So the print_names()
function has an arbitrary argument names
and by passing two strings into the function both of the strings are printed. Now enter the following code into your program.
print()
print_name("Tommy", "Dina", "Jesse")
Rerun your program and you should see the following added to the console.
Tommy
Dina
Jesse
So we can pass any number of strings into the print_names()
function.
5.2.3. Arbitrary keyword arguments **kwargs#
Keyword arguments are similar to arbitrary arguments with the exception that we can apply a keyword or name to each of the arguments.
def function(**kwargs)
commands to be executed
Note
Similar to *args
, the argument name **kwargs
is not a requirement and it could be any name after the **
. Again, it is common to use **kwargs
to remind someone reading the code that it is an arbitrary keyword argument.
To access the keyword arguments we can use kwargs[<argument>]
. To demonstrate this, enter the following code into your program.
# Arbitrary keyword arguments
def print_name_and_age(**kwargs):
print(f"Name: {kwargs['first_name']} {kwargs['last_name']}")
print(f"Age : {kwargs['age']}")
print()
print_name_and_age(age = 23, last_name = "Anderson", first_name = "Abby")
Here we are passing the three keyword arguments, age
, last_name
and first_name
into the function. Run your program and you should see the following added to the console.
Name: Abby Anderson
Age : 23
When using arbitrary keyword arguments the order in which the arguments are passed to the function does not matter. To demonstrate this, enter the following code to your program.
print_name_and_age(first_name = "Tommy", age = 53, last_name = "Miller")
Rerun your program and you should see the following added to the console.
Name: Tommy Miller
Age : 53
5.2.4. Ordering arguments#
When we combine standard arguments, arbitrary arguments and arbitrary keyword arguments we must add them to the function declaration is the following order
def function(argument1, argument2, *args, **kwargs)
5.2.5. Exercises#
Write a function called odd_or_even()
that outputs a message to the console stating whether an inputted number is odd or even. Use your function on the following:
2
5
0
Write a function called isprime()
that checks whether an inputted integer is a prime and prints the result. Use your function to determine which of the following are prime:
3469
6137
Write a function called mean()
that takes in the inputs of an unknown number of values and prints the mean value. Use your function to output the mean of the following numbers correct to 4 decimal places:
1, 2, 3, 4
5, 3, 7, 5, 8, 2, 4, 2, 1