Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
You can return values from your function, let's explore how to do that.
Learn more
- Python Standard Library (Don't get overwhelmed! There's a bunch of stuff waiting for you to use!)
- Python Docs - math module
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
You might have noticed that some
functions actually return a value.
0:01
When you call the function len and
pass it an object,
0:04
it actually gives you a value back.
0:07
In our case it gave us the number
of characters in our string.
0:09
Another example is when I call
the upper method on a string,
0:12
it created us a new string and
returned it.
0:15
Remember methods are really
just owned functions,
0:18
our string owns that function upper.
0:21
So another great feature of functions is
you can take some arguments, process them,
0:24
and then return a value to
the caller of your function.
0:28
So let's create a function
that returns a value.
0:31
Let's see, I know,
0:33
there are about a thousand apps
that do something similar to this.
0:37
You know when you're out to
dinner with a bunch of people and
0:40
you need to split the check
up between each other?
0:42
Let's write a function that
takes the total of the bill and
0:44
the number of people and then returns
the value that each person owes.
0:47
Okay, so let's create a new file and
we'll call our app, call the script,
0:51
let's call it check_please.
0:56
That's a good name.
0:58
All right, and
let's start defining our function.
1:01
So let's see, names are hard.
1:04
How about we define a new
function called split_check.
1:07
All right, and we're gonna need to
define a couple of parameters, right.
1:12
So let's see, we'll need the total of
the bill, so we'll call that total.
1:17
And we're also gonna need to have
the number of people involved in paying
1:23
this bill, right?
1:27
So to define multiple parameters for
a function,
1:29
you just separate them with a comma,
and then you just keep declaring.
1:32
So let's see, that's number_of_people.
1:36
I think that's all we'll need, right?
1:42
And then we don't wanna forget the colon
to signify that the body of our function
1:44
is coming up.
1:48
So how do we calculate this?
1:48
I guess if we just divide
the total by the number of people,
1:51
we should be pretty good, right?
1:55
So let's store that in a new variable.
1:58
So we'll say cost per person, Is
2:00
equal to the total / number_of_people.
2:05
That seems to make sense.
2:11
So this cost_per_person value
is what we want to give back
2:13
to whomever called this function.
2:17
So we want to return this value.
2:20
So, not surprisingly,
2:23
the key word to return a value
from within a function is, return.
2:24
So, what you do is you say return and
then the value.
2:30
So we're gonna return cost_per_person.
2:34
So now, let's use our new function.
2:38
So we know that the function is
expected to return the cost per person.
2:43
But in order to make sure that things
are clear here I'm gonna make a completely
2:47
different named variable.
2:52
Let's call it, let's call that amount_due.
2:54
I don't wanna confuse anybody.
3:00
So, again, you call a function,
3:01
we're gonna assign amount_due
to the result of this function.
3:04
So, you call the function, so
we'll say split_check, and
3:09
again you call it with parens.
3:13
And you put the arguments in here for
the defined parameters.
3:16
So for total, say that was $84.97.
3:20
And then for the second parameter, that
line's up, it was number_of_people, so
3:25
we'll do 4.
3:30
There were 4 people, it cost $84.97.
3:30
So now the result from split_check
is stored in amount_due.
3:35
Just like we saw in our notifications
here when we did this call to len(text).
3:42
So len returned
the number_of_characters and
3:48
we stored it in a variable
called number_of_characters.
3:51
The same thing here we
are calling split_check and
3:54
we're storing it in amount_due.
3:57
And remember when we call this,
this 84.97 gets set.
4:00
We can basically say, total equals
84.97 and then run this code and
4:03
then same thing here.
4:07
Just imagine this being
number_of_people equals 4, and
4:09
then when it's in here it's 4,
and this total is 84.97.
4:12
And our function returned
the value cost_per_person,
4:16
which we stored in amount_due.
4:20
So since we now have a value,
why don't we print it out?
4:22
So we'll say,
print("Each person owes ${}" and
4:26
then we'll go ahead and format that,
and we'll say (amount_due).
4:31
So let's run it, python check_please.
4:39
It worked but yikes, right, this is why
you don't wanna use floats for currency.
4:45
Now see these additional
fractions of a cent over here?
4:51
I'm pretty sure most people don't
have that extra $0.0025 on them.
4:55
In fact I'm pretty sure that most people
don't even have the $0.24 on them.
5:01
So what happens usually,
5:06
is whoever is paying the bill ends up
paying the extra money for everybody.
5:08
We should fix that in
our version of this app.
5:13
The fairest solution that I can think
of is to round up to the next dollar.
5:15
Now note, if we use the round
function here it would round down,
5:20
right, cuz $21.24 will go down to $21.
5:25
But I'm suggesting that we should pay $22.
5:28
As you can imagine,
there is a way to do this mathematically.
5:32
We could write it ourselves,
but you know what?
5:36
There's a function that exists for
us already.
5:39
The thing is though, we need to tell
our script that we want to use it,
5:42
it's not here by default.
5:45
In order to bring in this grouping of
new tools, we need to use a new keyword,
5:47
that keyword is import.
5:51
Here let's take a look at
it in the REPL real quick.
5:52
Now the name of the module or grouping of
tools that I want to use is called math.
6:01
We'll do a deeper look at modules and
how to create them in a future course.
6:08
But for now, we want to get our
hands on some math functions.
6:11
Okay so we're gonna import,
the name of the module is math.
6:16
And we can use functions in a module
by writing the module name, so math.
6:21
And then we use dot notation to
get to the function, so dot.
6:27
And the name of the function is ceil,
6:30
C-E-I-L, not the singer,
which is short for ceiling.
6:34
Basically what it does is always
rounds up to the next closest integer,
6:40
so let's do that.
6:44
So we'll do 21.24 is what we had right, so
this should round up to 22, and it does.
6:45
Now, of course, if you're interested,
you can take a look at all other functions
6:52
in the math module by doing help(math).
6:56
But again, don't feel like you need to
understand all of these functions yet.
7:01
I'm just showing you that you have a ton
of functions here for you just waiting for
7:05
you to call them, and
this is just one module.
7:09
Feel free to explore them,
you aren't gonna break anything.
7:12
More in teacher's notes.
7:16
I'm gonna drop out of the help,
I'm gonna drop out of the shell here too,
7:16
then I'm gonna clear.
7:20
So let's use that math module in
that ceiling function in our file.
7:24
So style suggests that we should put
all imports at the top of our file.
7:30
So I'm gonna do that and
we'll say import math.
7:35
And now that math has imported
we can just it when we need it.
7:40
And so what we want here is we want
the ceiling of this value, right?
7:43
So we could say,
math.ceil and there we go.
7:46
So what happens is this will run and
we'll get that 21.2455 whatever and
7:49
this will do the ceiling, it will
round it up to the nearest integer.
7:54
So let's go ahead, let's re-run that.
7:59
And boom, $22, nobody's getting
ripped off anymore, amazing.
8:03
So, I suppose the next step
to this is to allow for
8:08
our script to take dynamic
input from our users.
8:11
So, let's see, let's just do that.
8:15
We'll go here and
we'll say the total_due = ("what
8:16
is the total?"), give it some space.
8:22
Now that's gonna return a string,
remember, but we want it to be a float.
8:28
So let's coerce that, so we'll just
wrap this, we'll say float, awesome.
8:31
And next we need the number of people,
so let's coerce that to an int.
8:36
So we'll say number_of_people =
8:41
int(input( "How many people?")).
8:45
See how the braces line up there, awesome.
8:52
And now that we have those values,
we'll use them in our function call here.
8:55
So we'll say, let's just move this, we'll
get rid of this here, move this down here.
9:00
Say the amount_due is, so the total_due,
9:04
we'll pass that in there and
we'll pass in the number_of_people.
9:07
Here we go.
9:15
Let's run that real quick,
let's make sure that works.
9:18
What's the total, we got a 100
divided by 3 people, $34, awesome.
9:21
One more little bit of clean up here,
9:27
I wanna point out something
that I did intentionally.
9:29
Now see how in the function body here,
9:31
we are creating a variable
called cost_per_person.
9:34
And we never actually use it,
we just return it.
9:36
Well we can, and we should,
skip that unnecessary variable assignment.
9:40
It's a little strange to see it first,
9:45
which is why I used the variable
assignment originally.
9:46
So, we know that math.ceil is a function
that returns an integer, right?
9:49
So, I'm gonna take this, I'm gonna cut
this, and we'll paste this right here.
9:54
What we can do is we can
just return the result.
9:59
So you can read this like math.ceil
returns an integer value.
10:05
And then we return that value
to the caller of split_check.
10:12
I hope that makes sense,
10:19
there's no need to create that variable
just to turn around and return it.
10:20
So now we've got a super
functional function, right?
10:23
What could go wrong?
10:28
Well, as soon as you bring
users into the equation,
10:29
you'll start to see errors
that you didn't anticipate.
10:32
Like, let's go ahead and run this again.
10:35
Let's say that the bill total was 20 and
10:38
then something like this, four of us.
10:43
How many people, there were four of us.
10:47
Yuck, we should probably
handle that better.
10:49
That error is pretty intense for
a user, isn't it?
10:52
Watch this classic one.
10:56
We're gonna call the check_please.py,
and it was $40 and
10:57
I'm gonna put a 0 saying 0 people
are paying ZeroDivisionError, no!
11:03
If you don't remember from your
math class, you can't divide by 0.
11:09
It like opens a wormhole in outer space or
something intense like that.
11:12
We don't want our users doing that,
we don't want them seeing this.
11:16
So let's take a quick break and
then swing back into our script and
11:20
protect our users from themselves.
11:23
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