Learn about multiple parameters and return values: [ Ссылка ]
Welcome back! You're doing great at defining your own functions. Good job!
At this point, you already know how to define your own functions and even return values from them. What you'll learn next is how to pass multiple arguments to functions, as well as return not just one, but multiple values from them.
Let's tweak the square() function we've been working on a little bit more. Suppose that, instead of simply squaring a value, we'd like to raise a value to the power of another value that's also passed to the function. We can do this by having our function accept two parameters instead of just one. You should also change your function name AND docstrings to reflect this new behavior. raise_to_power is an appropriate function name.
Notice that there are now two parameters in the function header instead of one, value1 and value2. In the lines after that, the behavior of the overall function was also changed by raising value1 to the power of value2.
You can call the function by passing in two arguments because the function has two parameters, as declared in the function header. The order in which the arguments are passed correspond to the order of the parameters in the function header. This means that when we call raise_to_power(2, 3), when the function is executed, 2 would be assigned to value1 and 3 to value2. Looking at the function body, this means that the computation value1 to the power of value2 translates to 2 to the power of 3. This function call then returns the value 8.
You can also make your function return multiple values. You can do that by constructing objects known as tuples in your functions.
A tuple is like a list, in that it can contain multiple values. There are some differences, however:
Firstly, unlike a list, a tuple is immutable, that is, you cannot modify the values in a tuple once it has been constructed.
Secondly, while lists are defined using square brackets [], tuples are constructed using a set of parentheses ().
Here we construct a tuple containing 3 elements.
You can also unpack a tuple into several variables in one line. Doing so means that you assign to the variables a, b, and c the tuple values, in the order that they appear in the tuple. Additionally, you can also access individual tuple elements like you do with lists. Doing this here accesses the second element of the tuple. Why is that? Recall that with lists, you can use zero-indexing to access list elements. You can do the same thing with tuples! Pretty cool, right?
Let's now modify the behavior of your raise() function. Instead of returning just the value of value1 raised to the power of value2, let's also return the value of value2 raised to the power of value1. You thus need to make raise() return two values instead of one. We can use what we now know of Tuples to do this!
We first change the name of our function and the docstring to reflect the new behavior of our function. We then, in the function body, construct a tuple consisting of the values we want the function to return and, also in the function body, we return the tuple!
Calling the function constructed demonstrates that it does exactly what we want! Now it's your turn to play with writing function that accept multiple arguments and return multiple values. Enjoy!
Ещё видео!