Placeholder Image

字幕列表 影片播放

  • - Hello and welcome to Chapter 4, Functions.

  • This is the fourth of our basic patterns,

  • we'll get to iterations next.

  • Functions is the store and reuse.

  • One of the things in programming is that

  • we never like to repeat ourselves, we don't like to,

  • if we have four or five lines of code,

  • and we're going to do the same thing later,

  • we don't like to put the same four lines of code in,

  • even if...

  • It has to do with reliability.

  • If you find something wrong with those four lines of code,

  • and you've got them 12 different places in your program,

  • then you got to find all 12 places and fix them.

  • So collect those to one place,

  • and then call them and reuse them,

  • and that's the idea of store and reuse.

  • So, this is how functions work inside of Python.

  • And the first thing we notice is there is a new key word

  • "def" that stands for defined function,

  • and the def is like an if statement,

  • or we'll see fors and whiles,

  • that they end in a colon,

  • and then they have an indented block,

  • and then the indented block de-indents,

  • and that's the end of the function.

  • And so there's two statements make up this function.

  • The key thing that you have to understand and get used to

  • is this def part is actually not

  • running any code whatsoever.

  • It's actually remembering the code.

  • And that's what I call the "store phase."

  • The def creates a bit of code and records it,

  • like a macro, although it's much more complex than a macro,

  • and it names it whatever you chose.

  • You gave it a name.

  • We named this one thing().

  • And so it, as a side effect of Python

  • reading or parsing these three lines,

  • it doesn't do anything, but it remembers.

  • These two lines are what you would like

  • to run when you invoke thing().

  • So this is the definition of a function,

  • and this is the invoking of the function.

  • So this doesn't do anything.

  • So there's no output here from that stuff right there.

  • But then what happens is you invoke it.

  • And this thing() looks like it's part of Python, but you,

  • in effect, have extended Python with your def statement.

  • And so when it sees thing() it goes up and runs your code,

  • and so out comes "Hello Fun," and then it comes back,

  • and goes to the next line, does print(),

  • so print() comes out, and then it goes back like,

  • oh, this is the reuse part.

  • But we get to reuse it.

  • We define it once and we use it twice.

  • Then it runs this code again,

  • and then goes to the next line, and it's all done.

  • So this little bit came out twice.

  • And, of course, this is really simple,

  • so that I can fit it on a page,

  • but you get the idea that I don't wanna repeat.

  • This might be, you know, 15 to 100 lines of code,

  • and I don't wanna type those over and over again.

  • So I say, hey, store these in a name that I choose,

  • and then when I invoke them,

  • bring them back and then run them again.

  • So that's the basic idea.

  • We've actually already been using

  • functions from the beginning.

  • The print() is a function, right?

  • Print() is a function.

  • Every time we see print,

  • print() and then we have some stuff in here,

  • we're calling the print function.

  • This is the syntax, with two little parenthesis,

  • is the syntax for functions.

  • And so input is a function, type is a function,

  • float's a function, int's a function.

  • All these things are built-in functions

  • that come with Python at the moment that we started.

  • I mean, we installed Python and these came along.

  • And then there's other functions that we define and use,

  • and that's what the def is for.

  • In effect, we can create new reserved words,

  • of our own making, that extend the Python language.

  • After we define the function.

  • So it's just this bit of reusable code

  • that takes some arguments.

  • We haven't seen any with arguments,

  • there's a little parenthesis,

  • and we'll see how that works in a bit.

  • We define using the def keyword, and then we invoke it.

  • There's the defining phase,

  • which actually doesn't run the code,

  • it just remembers the code,

  • and then there's the invoking phase.

  • You define it once and then invoke it one or more times.

  • Calling the function or invoking the function,

  • we think of those two things as the same thing.

  • Call, invoke, are just the terms we use.

  • Most people just say call the function.

  • But invoking is a, perhaps,

  • more descriptive way to think about it.

  • So here's an example of a function.

  • It is built into Python.

  • It's called the max function.

  • And we can pass some parameters into the max function,

  • so we pass the 'Hello world' string.

  • Now, like much of Python, max knows what kind of thing

  • is being passed into it,

  • and it knows that it's looking for the largest character,

  • the lexicographically largest character.

  • And in this case, it scans this little

  • that's inside the max code.

  • It scans through and finds the largest character.

  • So, apparently, lowercase letters are higher than

  • uppercase letters because in English we get back a w.

  • And so this is what's called the return value.

  • So this is an assignment statement.

  • Let me clear this and start over.

  • So this is an assignment statement,

  • and so it has to evaluate this right-hand side.

  • And a function call is nothing more than, like, x + 1.

  • It's something to evaluate.

  • It runs the function code, passes in this argument,

  • and then this residual value, this is called the

  • return value, we'll look at this in more detail,

  • becomes the result of this little bit in the expression.

  • And there's nothing else.

  • We could have w + 1 or something.

  • And then the w is what's stored into big.

  • So we print big, and big is a variable

  • that has the letter w inside of it.

  • And then we ask, what is the smallest?

  • And that finds the blank, and so we get a blank

  • to see that there's a min function and a max function.

  • Both of these are built in.

  • These are built-in functions, they're always there for us.

  • Okay, so here is another example of the max function.

  • And so we can think of this as invoking, or calling,

  • this function, as this right-hand side is being evaluated.

  • We are passing this variable in,

  • and there's some code in here, and it's gonna do some stuff.

  • Yada, yada, yada.

  • And then it's gonna give us back a bit of stuff.

  • That's its return value,

  • and then that goes up into the big, right?

  • That's how this works.

  • And so this is actually built in.

  • Or burnt in. I guess I can't draw.

  • And so you can think of this as sometime,

  • a long time ago, when Python was being first formed,

  • somebody wrote some code.

  • And it's got some stuff in it,

  • and it's got a little loop that reads

  • through all the letters,

  • it has to figure out if it's a string or a list,

  • etc., etc., etc.

  • But this is store, except you didn't do the storing

  • because it's already built in, and then this is the reuse.

  • Store and reuse.

  • So we build these things into Python.

  • They're already prebuilt, as if,

  • before the first line of your code executes way up here,

  • someone put all this code in for you,

  • into Python, and created a thing called max for you.

  • Now we've been using this already.

  • Built-in functions, we've got type() conversions,

  • we've got the float() that takes an integer