Return to Video

Python for Informatics - Chapter 4 - Functions

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

more » « less
Video Language:
English, British
Team:
Captions Requested
Duration:
28:04

English subtitles

Revisions