"Database Foundations" was retired on March 22, 2017. You are now viewing the recommended replacement.
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
How about sending data to a function?
This video doesn't have any notes.
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
Welcome back.
0:00
Now that we've discussed
returning values from functions,
0:01
we're going to build on this by learning
how to send values to functions.
0:03
We've been using some pretty
simple examples so far.
0:07
Our function that adds two plus two
together wasn't very interesting,
0:09
even after we returned a value from it.
0:13
You might be thinking I still don't
get why functions are so useful.
0:15
Well, let's expand on this.
0:19
Our 2 plus 2 function
would be a lot more useful
0:21
if we could use it to add the number 2 to
any integer and we can totally do that.
0:24
Just like functions can send values
back via a return, functions can receive
0:29
values too and they can see, access and
use those values inside the function.
0:34
This is all done by what’s called
passing an argument to a function.
0:39
There are two parts to this.
0:43
First, when we call a function,
we can send a value.
0:45
Second, the function has to be written so
we can receive that value.
0:48
Have you ever wondered about those
pesky parenthesis we have to
0:53
add when defining or calling the function?
0:55
Those parens are where the sending and
receiving of values takes place.
0:58
Let's revisit our two plus two function.
1:02
Okay, first, I'm gonna change its name.
1:05
Instead of 2 + 2, I'll call it add_two.
1:07
This new name makes more sense because
we're gonna modify this function so
1:13
it now adds 2 to whatever
integer it receives.
1:17
Now inside the parens,
I'm gonna create a variable.
1:20
I'm gonna call it num.
1:23
Right now num has no value, it won't be
assigned a value until the function is
1:25
called and a value is passed but
we'll get to that in just one moment.
1:29
Now that we've created
a variable inside the parens,
1:33
this special kind of variable is
known as a parameter by the way.
1:36
We can use it inside of our function
just like any other local variable.
1:39
That means we can change the body
of our function like this.
1:43
What we've done here is rewrite our
function so that it can receive a value,
1:50
which is stored in the parameter
variable called num.
1:53
And then we can use that
value inside the function.
1:57
So how do we send that
value to our function?
2:00
Well, this happens when we call it.
2:03
First we'll write a regular function
call to the add_two function.
2:04
To send a value,
we add it inside the parens.
2:12
This value is called an argument.
2:15
An argument can be any Python data type or
expression that evaluates to a value.
2:17
This means you can pass in an integer,
a string, a function call, and
2:22
even other Python data types that
you haven't learned about quite yet.
2:26
In our case,
we're just gonna be passing an integer.
2:29
Let's pass the number 5.
2:32
Now, like we learned
earlier in this course,
2:35
not much will be visible to us
if we run our program right now.
2:37
To add some clarity to what's going on
behind the scenes, I'll add a print
2:40
statement inside our function that prints
out the value sent to the num parameter.
2:44
Now when I run this we can expect
the output to be five, let's double check.
2:53
Awesome, just as I expected the output
here is 5 because when we called
3:05
the add_two function we passed the integer
5 as an argument to the function.
3:09
This value was received by our function
and stored in the variable num.
3:15
Num, now referencing a value of five,
3:18
can be used like any local
variable inside the function.
3:21
I hope you're getting
the hang of arguments and
3:25
parameters when you're ready,
3:27
come join me in the next video to talk
about handling more than one parameter.
3:29
Also don't forget that you're doing great
and you're learning things that are going
3:32
to help you be more experienced and
capable programmer.
3:35
If you're struggling, it's okay.
3:39
Treehouse has tons of resources to help
you support you while you go through
3:41
your coursework.
3:44
The Treehouse community is an excellent
place to connect with other students and
3:45
ask and answer questions.
3:48
Watching videos a second time can
also help you absorb information.
3:50
Sometimes you really need
to hear things two or
3:54
three times before a concept
really starts to sink in.
3:56
Think about a time when you've watched
a movie a second or third time.
3:59
How much more did you pick up or notice?
Learning programming is a lot like that.
4:02
And finally don't hesitate to practice,
there is no wrong way to practice or
4:06
learn and trying things out in
your terminal is a great way to
4:10
understand what's happening in your code
without worrying about breaking anything.
4:13
Taking advantage of Treehouse's practice
sessions is another way to gain
4:18
a real advantage in
your skill development.
4:21
Remember, just have fun and always keep
in mind that even professional and
4:24
advanced programmers are always
learning and getting better too.
4:28
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