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

  • and returns a floating point version of that.

  • And, again, this is kind of like an expression.

  • So, it's like, I wanna divide this by 100,

  • but before I do that, I've gotta convert it to a float().

  • So it has to sort of do these function calls

  • as it's evaluating the expression.

  • Sometimes, like here, we just have a print()

  • out the return value, that's what this is,

  • this is the return value.

  • If you just type a function in a parameter,

  • it can be a constant or it can be a variable.

  • And as we'll see in a second,

  • we'll give you many of these, if you like.

  • So you can either just run it, or take the result of this.

  • This passes an integer and converts it to a float(),

  • and then puts the float() into that.

  • Type() tells us what kind of thing that is,

  • and you can use this inside of an expression,

  • so it's like, what am I gonna do first?

  • Oh I gotta do 2 times this thing.

  • Oh wait, pause just briefly for a moment,

  • call out to some float() code, pass a 3 into it,

  • and then something comes back.

  • The return value.

  • The residual value comes back,

  • and then that participates, in this case it's gonna be 3.0,

  • participates in this 2 * 3.0.

  • And so, 2 * 3.0 then ends up being 6.0, etc., etc.

  • But you can see, it's like,

  • oh wait a sec, I gotta figure out what this is.

  • Call the function, get the return value,

  • and then continue processing this expression.

  • We've also done this with string conversions,

  • partly because, just as an example,

  • the input always returns a string.

  • The input function returns a string.

  • And so here's this string, could be coming from input,

  • but we'll just take '123'.

  • We know that that's a string, it's not the number 123.

  • And if we try to add 1 to it, we get a traceback.

  • "Cannot concatenate 'str' and 'int'" traceback.

  • But we can convert that string to an integer.

  • And so int() can take a floating point number,

  • or an integer, or even a string,

  • and it says, oh I know what I'm supposed to do

  • with a string, I'm supposed to look at this,

  • interpret these as numbers, and, you know,

  • multiply by ten, and figure out what

  • the hundredths place is, and all that stuff.

  • There's a little bit of work to that and it does it.

  • But then it gives us back an integer,

  • and we say, oh, what is that?

  • That's now the 123, but it is of type int(),

  • and now I can add one to it and get 124.

  • And, as before, from this example that we're kind of

  • reusing from a previous chapter,

  • you don't want to try to convert...

  • Oops. Sad face. Sad face. Sad face.

  • You don't want to try to convert something

  • that doesn't have digits using int(),

  • because it'll say, "I don't know what to do",

  • and then your program quits.

  • You don't want your program to stop.

  • Tracebacks, you can, of course, deal with that,

  • and try and accept, but that's a previous lecture.

  • Okay, so up next we're gonna talk

  • about building our own functions.

  • Not just using the predefined ones.

- Hello and welcome to Chapter 4, Functions.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級 美國腔

PY4E - 函數 (第4章 第1部分) (PY4E - Functions (Chapter 4 Part 1))

  • 16 0
    劉馨兒 發佈於 2021 年 01 月 14 日
影片單字