An introduction to the use of Functions in Python
So far we have looked at functions with empty parentheses that do not take arguments, but we can define parameters in function definitions within their parentheses.
A parameter is a named entity in a function definition, specifying an argument that the function can accept.
Let’s create a small program that takes in parameters x, y, and z. We’ll create a function that adds the parameters together in different configurations. The sums of these will be printed by the function. Then we’ll call the function and pass numbers into the function.
def add_numbers(x, y, z): a = x + y b = x + z c = y + z print(a, b, c) add_numbers(1, 2, 3)
We passed the number 1 in for the x parameter, 2 in for the y parameter, and 3 in for the z parameter. These values correspond with each parameter in the order they are given.
The program is essentially doing the following math based on the values we passed to the parameters:
a = 1 + 2 b = 1 + 3 c = 2 + 3
The function also prints a, b, and c, and based on the math above we would expect a to be equal to 3, b to be 4, and c to be 5. Let’s run the program:
3 4 5
When we pass 1, 2, and 3 as parameters to the add_numbers() function, we receive the expected output.
Parameters are arguments that are typically defined as variables within function definitions. They can be assigned values when you run the method, passing the arguments into the function.
In addition to calling parameters in order, you can use keyword arguments in a function call, in which the caller identifies the arguments by the parameter name.
When you use keyword arguments, you can use parameters out of order because the Python interpreter will use the keywords provided to match the values to the parameters.
Let’s create a function that will show us profile information for a user. We’ll pass parameters to it in the form of username (intended as a string), and followers (intended as an integer).
# Define function with parameters def profile_info(username, followers): print("Username: " + username) print("Followers: " + str(followers))
Within the function definition statement, username and followers are contained in the parentheses of the profile_info() function. The block of the function prints out information about the user as strings, making use of the two parameters.
Now, we can call the function and assign parameters to it:
def profile_info(username, followers): print("Username: " + username) print("Followers: " + str(followers)) # Call function with parameters assigned as above profile_info("sammyshark", 945) # Call function with keyword arguments profile_info(username="AlexAnglerfish", followers=342)
In the first function call, we have filled in the information with a username of sammyshark and followers being 945, in the second function call we used keyword arguments, assigning values to the argument variables.
Let’s run the program:
Username: sammyshark Followers: 945 Username: AlexAnglerfish Followers: 342
The output shows us the usernames and numbers of followers for both users.
This also permits us to modify the order of the parameters, as in this example of the same program with a different call:
def profile_info(username, followers): print("Username: " + username) print("Followers: " + str(followers)) # Change order of parameters profile_info(followers=820, username="cameron-catfish")
When we run the program again , we’ll receive the following output:
Username: cameron-catfish Followers: 820
Because the function definition maintains the same order of print() statements, if we use keyword arguments, it does not matter which order we pass them into the function call.
We can also provide default values for one or both of the parameters. Let’s create a default value for the followers parameter with a value of 1:
def profile_info(username, followers=1): print("Username: " + username) print("Followers: " + str(followers))
Now, we can run the function with only the username function assigned, and the number of followers will automatically default to 1. We can also still change the number of followers if we would like.
def profile_info(username, followers=1): print("Username: " + username) print("Followers: " + str(followers)) profile_info(username="JOctopus") profile_info(username="sammyshark", followers=945)
When we run the program, we’ll receive the following output:
Username: JOctopus Followers: 1 Username: sammyshark Followers: 945
Providing default parameters with values can let us skip defining values for each argument that already has a default.
Write a program using one function that uses 2 parameters to pass in two numbers. The function should then demonstrate 3 maths processes using them.
Write a program using one function that uses keyword arguments to pass in two strings. The function should then use concatenation to combine them.
Write a program that uses 2 functions. One should pass direct parameters and the other should have keyword arguments. The purpose of the program is up to you!!
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.