Python for Absolute Beginners Transcripts
Chapter: Course conclusion
Lecture: Review: Functions
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
One of the most powerful tools we have
0:02
in our programming toolbox for changing the way
0:04
that we think about and work with our code
0:07
is functions.
0:08
Functions allow us to group together
0:10
a bunch of small little steps
0:11
into one very simple step.
0:13
Here we have a bunch of things going on
0:15
we have a function called play around.
0:16
It takes some arguments
0:18
and then we take a bunch of work inside the function.
0:20
But all that matters once we write this
0:22
when we want to use it
0:23
we know pass player_1 and player_2's name
0:26
and then what we'll get back
0:28
is the value of the winner.
0:29
Either player_1 or player_2
0:31
or if there's a tie, we get none.
0:33
And that allows us two really important things.
0:37
One, it allows up to think about
0:39
well this step is where you play the round.
0:40
We don't have to worry about the details.
0:42
Remember in our later part of the course
0:45
we changed the way that we got the input from the users.
0:48
We got that instead of just saying
0:50
input and type in a number
0:52
we actually used that autocomplete thing.
0:54
The stuff that was calling
0:55
get play or get role from the user
0:57
they didn't have to go and know
0:59
that we changed how that worked.
1:00
It just got changed and
1:01
everywhere we used it automatically
1:03
the better version got used.
1:04
It allows us to hide away
1:05
and not worry about the details
1:07
just used the high level idea
1:08
of playing around.
1:09
It also lets us organize our code
1:11
in a way that is much simpler.
1:13
We can think about here we're going to play around
1:15
we're going to check for the winner.
1:16
If there's enough winners.
1:17
I have enough wins for any given player
1:20
we're going to exit out of the loop
1:21
and they win.
1:22
It allows you to think at a very high level
1:24
instead of at every single bit
1:26
you're thinking about the details.
1:27
Super cool.
1:28
Be sure to make use of functions
1:30
try to keep them short and small.
1:32
If they get really long and cumbersome
1:34
you probably want to break that further
1:35
into smaller functions.
1:37
Allow you to get smaller and smaller detail as you go down
1:39
and not get things too complicated all in one place.