Python for Informatics - Chapter 4 - Functions
-
0:00 - 0:03Hello, and welcome to Chapter Four,
-
0:03 - 0:06Functions, in the book Python for
Informatics. -
0:07 - 0:10As always, these slides and this audio and
-
0:10 - 0:13this video are copyright Creative
Commons Attribution. -
0:15 - 0:19Now we are to the point, you know, Chapter
Four, we're sort of well into the class. -
0:19 - 0:24So I figure I should introduce myself a
little bit, let you know a little bit. -
0:25 - 0:27As I said before, I think in the beginning,
-
0:27 - 0:31we're tape, I'm taping this in a
-
0:31 - 0:36wonderful building at the University
of Michigan called the North Quad. -
0:36 - 0:40It's a relatively new building,
it's got some -
0:40 - 0:42residential sections and some academic
sections and some -
0:42 - 0:45classrooms, and one of the classrooms that
-
0:45 - 0:49I typically teach in is actually
2255 North Quad. -
0:49 - 0:51It's a really beautiful room
-
0:51 - 0:54with great ways for people to interact,
and so sometimes -
0:54 - 0:58I'm teaching, you know, little tiny
Dr. Chuck down here. -
0:58 - 1:00With a smile on the face.
-
1:00 - 1:04And sometimes my students are
taking me on, taking my -
1:04 - 1:09classes on campus and sometimes students
are watching me through lecture. -
1:11 - 1:14And so this building is really
beautiful and if -
1:14 - 1:16you ever get a chance to come to Ann Arbor
-
1:16 - 1:19and look at it, maybe walk through it,
it's really, it's really quite nice. -
1:19 - 1:21One of the things I like about it is
-
1:21 - 1:27that I think it's really highly
inspired by Harry Potter. -
1:27 - 1:30The kind of, of course, Oxford and
Cambridge are -
1:30 - 1:35the real inspiration for Harry Potter,
but our, our cafeteria, -
1:35 - 1:39for example, it kind of looks
like the four tables -
1:39 - 1:41in Hogwarts and you can kind of
imagine a snowy owl -
1:41 - 1:47flying around and a Sorting Hat at the,
at the front sorting people. -
1:47 - 1:53And so the nickname, the nickname for the
place is Quadwarts, because it's North Quad -
1:53 - 2:00Quadwarts, that's like Hogwarts and
North Quad kind of jammed together. -
2:00 - 2:02And of course, given that we sort of think
-
2:02 - 2:07of ourselves a little bit as Harry Potter,
-
2:07 - 2:11people when they first come in September
often sort of decide -
2:11 - 2:14to sort themselves and a few years back
-
2:14 - 2:17when we first started the building
the students -
2:17 - 2:22decided that I did not get to
be in Gryffindor. -
2:22 - 2:24As a matter of fact, it's probably
time for me to to -
2:24 - 2:28show you who I am, and who
I've been sorted to be. -
2:28 - 2:32So the students decided that
I couldn't be in Griffindor. -
2:32 - 2:34That I had to be in Slitherin,
-
2:34 - 2:37and that's because of
my name, Charles Severance. -
2:37 - 2:38And sever is snake.
-
2:38 - 2:40What's even cooler, of course,
-
2:40 - 2:46is given that I teach Python,
Slitherin's house is -
2:46 - 2:50a snake, right?
So it makes a lot of sense, I even have -
2:50 - 2:56this really fancy Slitherin teacup that I
use to drink tea during lectures. -
2:56 - 2:57Sometimes I drink coffee, and
-
2:57 - 3:00sometimes I drink tea.
Oh wow, this thing itches. -
3:00 - 3:02So let me just get rid of it.
-
3:02 - 3:04If I had any hair, that would
mess my hair up. -
3:04 - 3:06So let me get rid of this for the
rest of the lecture. -
3:09 - 3:13So there I am, okay.
Enough of that, back to, -
3:13 - 3:17back to Dr. Chuck.
[CLAP] -
3:17 - 3:25So, with that sort of brief, brief
interlude, the topic of -
3:25 - 3:29the actual topic of this lecture is
Functions. And so storing and -
3:29 - 3:34reusing is basically an idea that
-
3:34 - 3:38we will often have a series of
steps that we will -
3:38 - 3:42want to use over and over in a program,
increasingly complex. -
3:42 - 3:45The things that we'll use in this lecture
are kind of silly because -
3:45 - 3:48I have to keep them short so the slides
don't get too long. -
3:48 - 3:54But a good example of, you know, the kind
of work is maybe I am going to use -
3:54 - 3:59Google's geocoding service and I am
going to send some unstructured -
3:59 - 4:03data and get a GPS coordinate back.
-
4:03 - 4:05And that's a service that I want
to call and it would -
4:05 - 4:07maybe be about this much lines of, this
many lines of code. -
4:07 - 4:09And I'm going to want to do that
all over the place. -
4:09 - 4:14So do I want to put this many lines
of code 40 places in my program? -
4:14 - 4:15Or do I want to put it one place and
-
4:15 - 4:18then call it in the various places
that I need it? -
4:18 - 4:21And so that's why I call it the store and
the reuse function. -
4:22 - 4:25So if we take a look at the
simple syntax here. -
4:28 - 4:32These things are called functions and in some
languages it's called subprograms, -
4:32 - 4:37but we call them functions in Python.
And the keyword -
4:37 - 4:40that we're really going to focus on is
def, which stands for -
4:40 - 4:47define, and what happens here is
when Python sees this def keyword -
4:47 - 4:49it actually doesn't run the code.
-
4:50 - 4:53It says, oh, you're going to make a
function and you're -
4:53 - 4:57going to kind of turn on a recorder and
start recording this code. -
4:57 - 4:59So it has a colon at the end of it.
-
4:59 - 5:01So it has an indented block afterwards.
-
5:01 - 5:04And so the indented block becomes
recorded. -
5:05 - 5:07So instead of running the code,
like if we just -
5:07 - 5:10put print hello and print fun,
it would run it. -
5:10 - 5:15Instead it says, hey don't run it right
now, name it hello. -
5:15 - 5:16We give it a name.
-
5:16 - 5:18It's kind of like a variable.
We choose the name. -
5:18 - 5:21We've chosen hello as the name of this.
-
5:21 - 5:24Define it as hello, have it have these two
-
5:24 - 5:27lines of Python in it, and we'll
use it later, okay? -
5:27 - 5:30And so that's the function
definition. -
5:30 - 5:31That's the store phase.
-
5:31 - 5:35That is it's sort of like, it doesn't
really run those lines. -
5:35 - 5:37It sort of makes a variable called hello
-
5:37 - 5:40that actually contains Python code
-
5:40 - 5:43rather than containing like 12 or a
string or something like that -
5:43 - 5:45that we've worked with before.
-
5:45 - 5:50So this is the store part, and then the
reuse part is, we then have extended Python. -
5:50 - 5:57We now can call our bit of code. So we say,
hello, hello name is what we came up with, -
5:57 - 6:00parentheses, and then that says,
remember that code that I -
6:00 - 6:03put in there under the name hello?
-
6:03 - 6:06Run it now and, so, so, so if I
-
6:06 - 6:09start looking at that and then
it just continues. -
6:09 - 6:11So let me kind of clear this
and start over again. -
6:11 - 6:15And, so if I watch what Python does from
the beginning as -
6:15 - 6:18it reads here and it goes oh, you're
defining a function named hello. -
6:18 - 6:22Great, I will sort of remember, remember.
-
6:22 - 6:25I got that remembered for you, let's
continue on. -
6:25 - 6:26Oh, hello!
-
6:26 - 6:27You want me to run that stuff that
-
6:27 - 6:30you just got done storing under
the name hello. -
6:30 - 6:34So then it kind of goes and runs it and
out comes Hello Fun. -
6:34 - 6:39Then after that it runs to the print
and out comes print Zip. -
6:39 - 6:40And we say, you know what?
-
6:40 - 6:41I want to reuse that again.
-
6:41 - 6:44I stored it once, I can reuse it as many
times as I want. -
6:44 - 6:49And now, hello, and then these
two lines of code run a second time. -
6:49 - 6:52So we stored them once, gave them
a name, and then -
6:52 - 6:56ran them twice, in the context of
wherever it is we wanted. -
6:56 - 7:00Now this is not sort of a profound
-
7:00 - 7:03a profound reason to use it in this.
-
7:03 - 7:05I'm just trying to give you
the notion that there's a -
7:05 - 7:09way to store and name code that
then you can retrieve later. -
7:09 - 7:10That's really what's going on here.
-
7:12 - 7:13There's two kind of functions
inside of Python, and -
7:13 - 7:17we've actually been using them almost from
the very first lecture. -
7:17 - 7:19And that is there are built-in functions
that Python provides -
7:19 - 7:25to us like float, raw_input, int,
those kinds of functions. -
7:25 - 7:28Those are just part of Python but
we call them as functions. -
7:28 - 7:30The difference is we don't write them.
-
7:30 - 7:33And then there's user-defined functions,
functions that we write. -
7:33 - 7:37Functions that create functionality
that we want to make use of. -
7:37 - 7:43Like encapsulating the ability to compute
pay for time and a half for overtime. -
7:43 - 7:46And so, we name these things and we
-
7:46 - 7:48treat them as new reserved words that
we've created. -
7:48 - 7:50They're kind of an extension
to the language -
7:50 - 7:53as it were.
-
7:54 - 7:59So when we're coming along, we define a
function with the def keyword, right? -
7:59 - 8:00The def keyword
-
8:01 - 8:04is a reserved word. It's one of the many
reserved words back in Chapter One -
8:04 - 8:09that we talked about, and it indicates to
Python the beginning of a function. -
8:09 - 8:13We define it, and then when we call it,
which is called invoking, -
8:13 - 8:16it's like, we're building it,
and then we're invoking it. -
8:16 - 8:18And you can build it once, and then invoke
it many, many times. -
8:20 - 8:25So for example, here is a built-in
function called max that -
8:25 - 8:27finds the largest character, the sort
-
8:27 - 8:32of lexicographically largest character,
in a string. -
8:32 - 8:36And so it's like okay, tell me
the maximum character. -
8:36 - 8:37And so max is not some code that we've
-
8:37 - 8:41written but we are invoking a
function here and we're -
8:41 - 8:43passing in an argument to that.
-
8:43 - 8:48So the argument is this stuff
in between the parentheses. -
8:48 - 8:51So the max function can find the maximum
of many different things. -
8:51 - 8:55At this moment we want it to find the
maximum of that particular string. -
8:55 - 8:56The highest character
-
8:56 - 9:01in that particular string.
So, this is a left, a right-hand side of -
9:01 - 9:05an assignment statement too.
So that has to be evaluated to a value. -
9:06 - 9:08So it goes into the function,
-
9:08 - 9:10does whatever things the function
wants to do, -
9:10 - 9:12and then the function
gives us back a value -
9:12 - 9:17that becomes the value for
max parentheses Hello world. -
9:17 - 9:20And that value in this case is
the letter w, okay? -
9:20 - 9:22Because the letter w was
decided to be -
9:22 - 9:25the highest letter and that's
what max gives us back. -
9:26 - 9:31And then when we're done with that,
then that w ends up being assigned. -
9:31 - 9:32The assignment statement completes.
-
9:32 - 9:34And so you can think of the
function evaluation -
9:34 - 9:39as happening as part of the right-hand
side expression calculation. -
9:39 - 9:41There could a plus here and other stuff
-
9:41 - 9:43and it's just, at some point,
a big expression. -
9:43 - 9:46And this one, it's a simple expression
with just one function call. -
9:49 - 9:51Now if we look at this, there is some
code somewhere. -
9:51 - 9:53Somebody wrote some code.
-
9:53 - 9:54It's part of Python.
-
9:54 - 9:58You didn't write it.
There's a max function somewhere. -
9:58 - 10:00And you can think of a function
as having some input. -
10:00 - 10:02It's kind of like a program, that's why
-
10:02 - 10:05some languages call these things
subprograms. -
10:05 - 10:08Because they have an input, they do some
kind of useful works, whatever -
10:08 - 10:13that useful work happens to be, and then
they produce some kind of an output. -
10:13 - 10:14Right? So Hello world
-
10:14 - 10:17is the input, a string, the
arguments, the thing we're passing in. -
10:17 - 10:21Hello world is what's being
passed in to the function. -
10:21 - 10:25The function is running and then something
comes back and is sent back. -
10:25 - 10:30So it has input, processing, and output.
-
10:30 - 10:32Input, processing, and output.
So that's how a function. -
10:32 - 10:35Some stored code, whether we
wrote it or not, -
10:35 - 10:38they work the same when we call
-
10:38 - 10:39functions, right?
-
10:39 - 10:44So you could think of this as, somewhere
inside of the Python library -
10:44 - 10:47is some code that maybe has a little
def in there, and the name, -
10:47 - 10:51they named the function max, and it
takes a single parameter. -
10:51 - 10:54And it does some blah, blah, blah, blah,
-
10:54 - 10:59loopy blah, blah stuff, whatever
max wants to do. -
10:59 - 11:01Whatever we need max to do based on
-
11:01 - 11:03the specifications that max is
supposed to support. -
11:03 - 11:04But somewhere
-
11:04 - 11:07there is code inside of Python
-
11:07 - 11:09that actually represents the
function definition. -
11:09 - 11:12It's a built-in function because it
comes with Python -
11:12 - 11:15and we didn't have to do
anything to add it. -
11:15 - 11:18So some common built-in functions
that we have -
11:18 - 11:22been using all along, good examples
are the float, which -
11:22 - 11:26takes as input anything and
-
11:26 - 11:29returns you a floating
point number version of that. -
11:29 - 11:32Type, which takes a parameter
of a variable or a constant -
11:32 - 11:35and says, what is the type of this.
-
11:35 - 11:38Float, again converting.
-
11:38 - 11:39Type, again, and float.
-
11:39 - 11:42So these are all things that, we've been
calling functions all along. -
11:42 - 11:46And it passes the input value
into the function, the -
11:46 - 11:49function runs and then gives
us back a return value -
11:49 - 11:54which then participates in the rest of the
expression on the right-hand side. -
11:54 - 11:56You can think of it's pausing the calculation
-
11:56 - 12:00on the right-hand side, calling the function,
-
12:00 - 12:01getting the result of the function back,
-
12:01 - 12:04and then continuing the evaluation of the
right-hand side. -
12:04 - 12:08Then coming up with whatever value and
then printing that value out. -
12:08 - 12:13Okay?
Another thing that we've done -
12:13 - 12:15is we've done string conversions.
Right? -
12:15 - 12:18So we've converted, in this case,
a string to an integer. -
12:18 - 12:20And asked what type it is.
-
12:20 - 12:24We've converted a string to an integer.
So, int converts its argument, -
12:24 - 12:27whatever that happens to be,
into an integer. -
12:27 - 12:28So that's just some of the built-in
-
12:28 - 12:31functions that we have talked about
so far. -
12:31 - 12:34Now, this becomes more interesting
-
12:37 - 12:39when we can make our own, own functions.
-
12:39 - 12:44Oops, there goes my teabag right in the
middle of the thing. -
12:45 - 12:50Got to take the teabag out, I think it's,
whoa, hang on, be right back. -
12:56 - 13:03Teabag, okay, there's my tea.
So, so we want to make a new function. -
13:03 - 13:06Like I said in the other example,
we use the def keyword, -
13:09 - 13:12the def keyword here, and then we have
some indented bit. -
13:12 - 13:16We create a name for it and then
have some parentheses. -
13:16 - 13:19These parentheses will later tell
the inputs -
13:19 - 13:20that we're going to pass in, but this
-
13:20 - 13:25function has no input, so we just go
parenthesis, parenthesis and then the -
13:25 - 13:28all-important colon character which
indicates the beginning of an -
13:28 - 13:32indented block of Python, that then is,
this detects the function. -
13:32 - 13:35So, it's important to remember
-
13:35 - 13:38that while this is executing
when Python first -
13:38 - 13:41looks at this, it doesn't run
these lines of code. -
13:41 - 13:45It just remembers them, and names them
print lyrics. -
13:45 - 13:49So it doesn't cause any printout, it just
causes Python to remember. -
13:49 - 13:54I probably said that a few too many times.
So, so here is -
13:54 - 13:59a difficult problem, and I'll let you
think about -
13:59 - 14:00it for a while.
-
14:00 - 14:03I want you to kind of mentally go through
and execute this code. -
14:05 - 14:11And ask what, ask yourself what the output
of this program would produce. -
14:12 - 14:13How many lines?
-
14:13 - 14:16How many lines of output would this
program produce? -
14:22 - 14:27So, how many of you said three?
-
14:28 - 14:34How many of you said five?
Well, the right answer -
14:34 - 14:40is actually three. You see five
print statements, two, three, -
14:40 - 14:46four, five, but two of the print statements
are sitting inside of this. -
14:46 - 14:51And we never called, we never invoked a
function down here, okay? -
14:51 - 14:52So, this one,
-
14:54 - 14:59let's clear this.
This one prints, these two get skipped, -
14:59 - 15:01this one prints, and this one prints.
-
15:01 - 15:04So that that's why there are three
statements that print. -
15:05 - 15:10There is stored, but we never used, a
function called print lyrics. -
15:10 - 15:14And it's got two statements in it, but we
never used it. -
15:14 - 15:20So the output of this is
Hello Yo 7, and that's because we -
15:20 - 15:21never actually invoked it.
-
15:21 - 15:27We had to say print lyrics parentheses
or whatever to cause it to call this. -
15:27 - 15:29Okay? That's just to emphasize that as it
-
15:29 - 15:32looks at it, it does not execute
these lines. -
15:34 - 15:39So once we've defined a function, once we
have given it a name, given it -
15:39 - 15:45code that is a part of it, then we can invoke
it or call it as many times as we like. -
15:45 - 15:45So now,
-
15:45 - 15:49our little example works a little better
if we actually call our function. -
15:51 - 15:54Python really doesn't care if you
don't call your function. -
15:54 - 15:56It's like you told me to
make one, I made one. -
15:56 - 15:58You didn't use it.
There you go. -
15:58 - 16:03But if you look at this one now,
so here we go, x equals 5. -
16:03 - 16:06print Hello, out comes Hello.
-
16:06 - 16:06Define.
-
16:06 - 16:08Nothing happens here.
Nothing happens here. -
16:08 - 16:09It's just remembering.
-
16:11 - 16:13Okay? Then it says, print Yo.
-
16:13 - 16:16Then it calls the function print lyrics,
which sort of stops us here, -
16:16 - 16:21runs these two lines of code.
So out comes that and that. -
16:21 - 16:27Then it sort of finishes this and it comes
back, x equals x plus 2, then it prints x. -
16:27 - 16:29That must mean that x is 7,
and so out that comes. -
16:29 - 16:35And so, again, it's on the
first time through. -
16:35 - 16:37No, go back, go back, go back.
-
16:38 - 16:40On the first time through, it
doesn't print. -
16:40 - 16:42But then when it hits this,
-
16:42 - 16:43it prints.
-
16:43 - 16:45You could say print lyrics
several more times and -
16:45 - 16:47it would run this as many times as it did,
-
16:47 - 16:51and it needed to, as many times as you
want, and it would make output for you. -
16:51 - 16:55So you can invoke this is the definition,
let's clear this. -
16:57 - 17:03This is the definition.
This is the call or invoke. -
17:04 - 17:07So we are invoking the function,
we're calling the function, -
17:07 - 17:10we're causing the function to execute.
-
17:10 - 17:14Here we're just causing the function
to be looked at and defined, -
17:14 - 17:17but not actually executed.
Hope that's clear. -
17:17 - 17:23Now, when we pass data into a
function, and functions -
17:23 - 17:26that don't take data are not as
useful as they could be. -
17:26 - 17:28There are plenty of things that do, times
-
17:28 - 17:30that you build a function doesn't take
data. -
17:30 - 17:32But the most interesting functions
are the ones -
17:32 - 17:34that you could hand them something to
work on and -
17:34 - 17:37they could do their work and then come
back with whatever. -
17:37 - 17:42So this max function is a good example of
this, one that's taking an argument. -
17:42 - 17:44We call the things in between the
parentheses -
17:44 - 17:46when we're invoking the function,
-
17:46 - 17:51we call the things in between the
parentheses arguments, okay? -
17:51 - 17:53So that's passing into the function.
-
17:53 - 17:57Feeding data into the function.
So we put arguments in between them. -
17:59 - 18:03So for example, here we have
a little program. -
18:03 - 18:04That
-
18:06 - 18:09that is, it's a function named greet, and
now we are -
18:09 - 18:12going to define this function and we're
going to say, you know what? -
18:12 - 18:16I would like to take a parameter, let's
take a parameter. -
18:16 - 18:18Let's have one parameter come in.
-
18:18 - 18:20And we need kind of a placeholder for that
-
18:20 - 18:23parameter, so within the function we're
going to use lang. -
18:23 - 18:25Now this isn't actually a real variable.
-
18:25 - 18:28It's kind of like a, it's a placeholder
variable. -
18:28 - 18:31So this first parameter, whatever it is,
-
18:31 - 18:33when it's called, is going to be lang.
-
18:33 - 18:37And so if that first parameter is
equal to es, -
18:37 - 18:39we're going to print Hola.
-
18:39 - 18:43And else if it's equal to fr
we'll print Bonjour. -
18:43 - 18:44And otherwise, we'll print Hello.
-
18:44 - 18:49So there's apparently three languages in
the world, Spanish, French, and English. -
18:49 - 18:52And if it's not Spanish or French, then it
must be English. -
18:52 - 18:56But, I, you have to keep this kind of
small, so my screen doesn't get too big. -
18:56 - 18:59So this is again just the definition and
if you type this -
18:59 - 19:02into the interactive thing it gives you
this dot dot dot prompt. -
19:02 - 19:04And so we now have this thing called greet
-
19:04 - 19:08and now we've extended Python to add
our own function to Python. -
19:08 - 19:13And now we can say greet en and so
it runs this code -
19:13 - 19:19except that en is lang and so that comes,
and then it prints Hello. -
19:19 - 19:21So out comes Hello.
Now later we can say, oh, -
19:21 - 19:26I would to do a greeting, but this time
I'm going to pass es in as it. -
19:26 - 19:32So lang becomes, for this execution, es.
And then, so it prints out Hola. -
19:32 - 19:39And then the next execution, lang is fr.
So it executes this three times but -
19:39 - 19:44lang is different each time because we've
passed in different parameters each time. -
19:44 - 19:47So that's how we can kind of write
general-purpose code -
19:47 - 19:53inside the function and then reuse that
general-purpose code in different ways. -
19:53 - 19:55Okay? It's a real powerful, powerful
mechanism -
19:55 - 19:57that makes functions far more useful.
-
20:00 - 20:05Now, functions don't necessarily
just have to do stuff. -
20:05 - 20:08A real powerful mechanism in a function is
what we call a return value. -
20:11 - 20:14So a function can take its
arguments, do some work. -
20:14 - 20:15We've seen that.
-
20:15 - 20:21And then it can return a value. And the key
to the return value is, when we call the -
20:21 - 20:23function, like we were calling max,
-
20:23 - 20:27it gives us back some value like the
little w, okay? -
20:27 - 20:33So here we're going to make a function
called greet that takes no parameters. -
20:33 - 20:35Doesn't take parameters, but it has
another keyword. -
20:35 - 20:38It's another reserved word in Python
-
20:38 - 20:41and whatever we put on this
-
20:41 - 20:47return statement shows up as the
replacement in this expression. -
20:47 - 20:51So, whatever greet is, it runs greet and
-
20:51 - 20:53then the return is kind of a
residual value. -
20:53 - 20:56So if we say print greet, comma, Glenn.
-
20:56 - 20:58it says Hello Glenn, because
the return value -
20:58 - 21:01for the greet function
is the string Hello. -
21:01 - 21:03And if we say greet Sally,
-
21:03 - 21:05it says Hello Sally.
-
21:05 - 21:10And so, and it's run the code twice and
the return function, return value has -
21:10 - 21:14been put in here instead. And so the Hello
came there and the Hello came there, -
21:14 - 21:16so we get the two lines.
-
21:16 - 21:19So, return is a statement that both
terminates the -
21:19 - 21:23execution of the function and
defines the value of -
21:23 - 21:26what will be replaced when
the function call comes back, -
21:26 - 21:28in the line that the function was
called from. -
21:30 - 21:35So here is a little smarter version of
our greet function. -
21:35 - 21:40It's very similar, it's called greet
still, takes lang as a parameter. -
21:40 - 21:44And if the language is es, then
it returns the string Hola. -
21:44 - 21:47If the language is French,
it returns Bonjour. -
21:47 - 21:48Otherwise, it returns Hello.
-
21:48 - 21:50So we're not actually doing
the print, if you -
21:50 - 21:52go back on the other slides,
we were printing. -
21:52 - 21:56But now we're just returning a string.
Okay? -
21:56 - 22:01And so now, I can call print greet,
and pass en in, -
22:01 - 22:04so then that runs the code once,
with lang equal to en. -
22:04 - 22:07And I get back Hello, and then
comma, Glenn. -
22:07 - 22:09Then I call it again and I pass es in.
And then -
22:09 - 22:15that time it returns, the return value
here becomes Hola, a string Hola. -
22:15 - 22:20Hola Sally. And then Michael,
I'll pass in one more time. -
22:20 - 22:21Lang is now fr,
-
22:21 - 22:26the string fr, and so it returns a
Bonjour and -
22:26 - 22:32so the, the residual that is here is
Bonjour, and so out comes Bonjour Michael. -
22:32 - 22:34So there is lot to this, right?
-
22:34 - 22:35You're passing stuff in.
-
22:35 - 22:38You have this kind of placeholder
variable. -
22:38 - 22:42And you have this return that sort of
appears where it was called from. -
22:42 - 22:46This goes in, does its work, it comes back,
and there's sort of the -
22:46 - 22:47residual value that sits here.
-
22:47 - 22:51You don't have to have a return in a
function, but if you want to -
22:51 - 22:55do something with the value, then you have
to have a return in the function. -
22:55 - 22:58We call the functions that produce values
-
22:58 - 23:02fruitful, and the other ones
are called void. -
23:02 - 23:04[LAUGH] So, that's a good name for them.
-
23:06 - 23:08So, to review sort of this, arguments,
-
23:08 - 23:11parameters, and results, if we
look at max, -
23:11 - 23:16the original thing, where it's looking for
the largest, lexicographically -
23:16 - 23:22largest letter, it looks Hello world is
the argument that's passed in. -
23:22 - 23:25We have this sort of formal parameter here
called inp which is not really a -
23:25 - 23:28variable, it just happens to refer to
whatever is the -
23:28 - 23:32first argument in any particular call.
-
23:32 - 23:36And then it does its little thing and
runs loops and does all these things and -
23:36 - 23:41at some point it returns w, so that the
thing that comes out when the function -
23:41 - 23:47quits that becomes the replacement value
here is a lowercase w string. -
23:47 - 23:50And then that is the w that
goes over in the big. -
23:50 - 23:55So the return is what defines what comes
back here. -
23:55 - 23:59Because you think of this as, it's looking
at this. It suspends for the moment. -
23:59 - 24:00It runs this code.
-
24:00 - 24:01It's holding.
-
24:01 - 24:02It's holding itself here.
-
24:02 - 24:05It's running this code and then
it comes back to here. -
24:06 - 24:07Okay?
-
24:07 - 24:10And the return value is what defines
coming back. -
24:11 - 24:18So, of course you can have more than one
parameter and they are in order. -
24:18 - 24:19So here we have an a and a b.
-
24:20 - 24:22These, the name of these things doesn't
really matter. -
24:22 - 24:26They're just relevant inside of the
function definition. -
24:26 - 24:28So we are going to add
two numbers together -
24:28 - 24:32by taking a + b, and then
returning the sum. -
24:32 - 24:34The added variable is just kind of local
to this function. -
24:34 - 24:38And now we can say, you know,
addtwo, 3, comma, 5. -
24:38 - 24:40And then this will come back as 8,
and then 8 will get assigned -
24:40 - 24:43into x, and so that will
print out 8. -
24:43 - 24:45And so you can have as many of
these as you want -
24:45 - 24:49and the order matters and there is a
one-to-one correspondence. -
24:49 - 24:513 goes to a and 5 goes to b
-
24:51 - 24:52when the thing is called.
-
24:52 - 24:56And then the return value again
comes back. -
24:56 - 25:01Okay?
So that's, sort of, arguments. -
25:01 - 25:05And like I said, not all functions
have to return values. -
25:05 - 25:08We call them void functions when they
don't return anything. -
25:08 - 25:11It's totally fine for that to be the case.
-
25:12 - 25:16So at this point you might be thinking
to yourself, okay, great, well I still -
25:16 - 25:18don't quite get why to use functions.
-
25:18 - 25:23And in reality, in the first 10, 11
chapters of this book, other than -
25:23 - 25:26using lots of functions, we're not
really going to spend a lot -
25:26 - 25:30of time making functions because most of
our programs are going to kind of be -
25:30 - 25:34that long and we're not going to
do a lot reuse in the program. -
25:34 - 25:39And there'll be a time when your
programs become complex enough. -
25:39 - 25:41You'll be like, oh, thank heaven for
functions. -
25:41 - 25:45I think it's premature to say you
must use functions, even though -
25:45 - 25:48there are some exercises that just say,
hey, do this with a function. -
25:48 - 25:50Just so you kind of get the
understanding of a function. -
25:51 - 25:52You will find soon enough,
-
25:54 - 25:56as your programs grow, you'll go like, oh,
-
25:56 - 25:57I keep doing this same thing
over and over again. -
25:57 - 25:59Let me pull it up into a function and pass
-
25:59 - 26:03a parameter in, have a return value, and
away you go. -
26:03 - 26:05Or, you might find that you're moving from
-
26:05 - 26:06one program to another and you have this
common thing -
26:06 - 26:10that you want to do, so you make yourself
a library that you drag along. -
26:10 - 26:12And we will do lots of libraries.
-
26:12 - 26:15The book in the second half does
lots and lots of library -
26:15 - 26:19stuff, doing things like parsing XML and
this, that, and the other thing. -
26:19 - 26:24So, so don't feel like you need to use
functions on every -
26:24 - 26:28assignment, because they're a natural
thing when a program gets big enough. -
26:28 - 26:31So, so just kind of understand them on a
-
26:31 - 26:33mechanical level, but
-
26:33 - 26:35it'll come to you at the right time
-
26:35 - 26:38when it's time to start building
your own functions. -
26:38 - 26:41So in this class, we kind of, you know,
talked about functions. -
26:41 - 26:42Just got you started.
-
26:42 - 26:45Talked about parameters, talked about
built-in functions, -
26:45 - 26:50talked about return values, the
store and reuse pattern. -
26:50 - 26:53So, the problems at the end of the
-
26:53 - 26:57chapter for this particular chapter
are relatively straightforward, -
26:57 - 27:01in that, like I said, we don't have
a real strong -
27:01 - 27:06need to do functions yet in this class
because the programs aren't large enough. -
27:06 - 27:08But I just said okay, take, take one
-
27:08 - 27:12of your previous assignments and
refactor the code -
27:12 - 27:17so that at the top there is a def
computepay, and you put like the if -
27:17 - 27:20and whatever in here, and then later on
-
27:20 - 27:22you do your code and then you call
computepay. -
27:22 - 27:24So you took code that you already had, you
-
27:24 - 27:27move it up into a function, and
make a function. -
27:27 - 27:29And I've also online got sort of a
-
27:29 - 27:32sample of this, because it's, it's
a little complex. -
27:32 - 27:37And so you should be able to find, on
Python Learn or on the course site, -
27:38 - 27:40you should be able to find a good example,
-
27:40 - 27:42because I really want you to
sort of get this. -
27:43 - 27:45Like I said, there will
come a time -
27:45 - 27:48when functions will make the most
sense to you. -
27:48 - 27:52But coming up next, of course, is
Chapter Five, and that's loops. -
27:52 - 27:55And loops are going to rock the house,
And so we really, that's our -
27:55 - 28:01fourth major pattern is loops and
and I'm looking forward to it. -
28:01 - 28:03So we'll we'll see you at the next lecture.
- Title:
- Python for Informatics - Chapter 4 - Functions
- Description:
-
This is a lecture from Python for Informatics - www.pythonlearn.com
All Lectures: http://www.youtube.com/playlist?list=PLlRFEj9H3Oj4JXIwMwN1_ss1Tk8wZShEJ - Video Language:
- English, British
- Team:
- Captions Requested
- Duration:
- 28:04
Claude Almansi edited English subtitles for Python for Informatics - Chapter 4 - Functions |