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: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.
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.
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.
Become a Member to join the conversation.