Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Functions, Packing, and Unpacking!
You have completed Functions, Packing, and Unpacking!
Preview
What happens when you need to send more than one argument to a function?
Solution
def multiply_two( num1, num2 ):
val = num1 * num2
return val
print(multiply_two( 5, 7))
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, now that we've tackled sending a
single value to a function with arguments
0:00
and parameters, let's talk about
sending multiple values to functions.
0:04
The truth is,
there really isn't much of a difference.
0:09
You can send as many arguments
to a function as you want, so
0:11
long as it has a corresponding parameter.
0:14
You simply separate the values with
a comma when you call the function, and
0:17
separate the parameters with
a comma in the function definition.
0:20
Let's revisit the add_two function and
expand on it.
0:23
To recap, the add_two function
receives one argument.
0:27
Stores the value of that
argument in the parameter num.
0:30
Then adds two to that parameter.
0:34
Then it returns the sum.
0:37
What if instead we wanted a function that
would add any two integers together?
0:39
To do this we could send two integers
to the function when we call it and
0:44
rewrite or function a little so
that it can receive two arguments.
0:47
Just follow along with me here and in a
little while you'll have an opportunity to
0:51
open up your workspace and
try by yourself.
0:55
First, we'll modify the function so
we can receive a second parameter.
0:57
I'll change this first parameter to num1
and then add a second one called num2.
1:03
As you can see, all I did to make this
happen was add a second parameter
1:07
separated from the first by a comma.
1:12
It's important to note that no two
parameters can have the same name.
1:16
If you give two parameters the same
name the Python interpreter will send
1:19
an error back.
1:22
It's also very important to know that the
arguments will be received in the order
1:24
that they are sent.
1:28
The first argument in the function call
will always be received into the first
1:28
parameter in the function definition.
1:33
This is why these are called
positional arguments,
1:36
the order in which you send them matters.
1:38
The position of the argument must
correspond with the position
1:40
of the parameter.
1:44
Okay, now, in the body of the function
let's edit the expression that's assigned
1:45
to the val variable, so
it adds these two new parameters together.
1:49
Also, since the function was updated and
does something different now,
1:59
I'll also change the function name
to something that's more accurate.
2:02
And finally, let's fix our function call
to reflect the new function name and
2:09
the additional parameter.
2:13
And so, for the second argument I'll
just pick the integer 10 to send.
2:18
Now, I'm gonna add a print statement
around our function call so
2:21
we can see the outcome of our changes
when we save and run the program.
2:25
All right, now, I'm gonna save and
run down in our terminal.
2:33
Awesome, it printed out 15 which
is in fact the sum of 5 and 10.
2:47
Now, it's your turn.
2:51
Open up the attached workspace.
2:53
Inside you'll see the unmodified add two
function and a call to that function.
2:55
After pausing the video here try to
change up the add_two function so
2:59
that it receives two arguments.
3:03
The body of the function should multiply
these two arguments together and
3:05
then it should return that value.
3:09
After that edit the function
called to reflect the new number
3:10
of required arguments and
the new function name.
3:14
When you're done check the teacher's
note for the solution and
3:17
then unpause the video.
3:20
Welcome back,
I hope that was a fun challenge for you.
3:21
How did it go?
3:25
Before moving on the the next stage,
3:26
why don't you take a few moments to
play around with your updated function.
3:27
What happens if you don't
pass two arguments?
3:31
What happens if you pass
a string instead of an integer?
3:33
Try adding a third parameter and argument.
3:36
Have fun with it and poke around
before joining me in the next and
3:39
final stage of this course.
3:42
Keep on coding.
3:44
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up