00:00 In this lesson, you’ll learn the fundamentals of passing argument values to function parameters. First, let’s review some concepts. A function is a named block of code designed to perform some specific task.
00:15 An argument is a value provided to the function for the function to use in some way. The argument is stored in the function with a variable often called a parameter or parameter variable.
00:28 I will try to be consistent and refer to the value given in a function call as an argument and the variable used in the function definition as a parameter.
00:38 You’ll see other resources that use those two terms interchangeably, and you’ll even see a few that switch the meanings around, but this is the way that I’ve used to use these two terms, and you’ll see that a lot elsewhere as well.
To pass an argument to a function means to give that argument value to its associated parameter variable. So in this example, you would pass an argument for the parameter
num. This function would then take that value, multiply it by itself—essentially squaring it—and then return that squared result.
Here’s a Python file with that function and a script to test it out. When run, the script will give the variable
val a value of
4 and then call
square() on that variable’s value, which is then printed.
01:32 We can run the script directly
01:38 and see the result, or we could do this interactively.
01:45 I’ll go ahead and import that module,
and ptpython wants to go ahead and run the script that’s in it. Well, we want to look at this interactively, so we can create our own variable
val, set it equal to
square() on that variable’s value. You get the result
16. Of course, we can use a constant to test this out as well, or anything else you’d like to try.
What’s happening? The value
4 is being provided as an argument to the function
square(). When the function is called, the parameter variable
num is given the value of
4 and then used in the rest of the function. Let’s look at the function definition again.
This might seem silly to even mention, but we’d never even consider changing the value of
num. Once the function begins executing, it contains the number we want to square.
We don’t need to change
num to something else. Now, I know there are some functions that implement certain algorithms that require modifying one of the parameter variables for the algorithm to work, but those are very special cases and they make sense when you see them.
Most importantly, the variable
val is still
4 when this function is done. And even those rare functions that have to modify the parameter value to work, the argument’s value outside of the function won’t be changed once the function ends. Now, I realize you may have seen a function or two whose job it was to actually modify an argument. We’re not talking about that now. We’ll get to that, but that’s not the point of this lesson.
03:40 To understand what Python does with argument passing, it might help to look at other languages first, since many of them have simpler processes than Python does.
Become a Member to join the conversation.