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.
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:
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:
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).
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:
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:
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:
When we run the program again , we’ll receive the following output:
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:
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.
When we run the program, we’ll receive the following output:
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.