Return to Video

Return Statement - Intro to Computer Science

  • 0:01 - 0:03
    So now that we know the inputs and the outputs,
  • 0:03 - 0:06
    the procedure almost writes itself. Especially since we already know the
  • 0:06 - 0:10
    code that we wanted to execute before we defined the procedure.
  • 0:10 - 0:13
    So I'm going to write the procedure, by modifying the code here.
  • 0:15 - 0:19
    So first what we're going to do is make a little space.
  • 0:19 - 0:21
    Erasing the code that's not part of the 4 lines that we
  • 0:21 - 0:24
    want to turn in the procedure. So now I've, I haven't
  • 0:24 - 0:28
    modified the code at all. What I have is, almost exactly what
  • 0:28 - 0:30
    I need for the body of the procedure. This is what
  • 0:30 - 0:33
    we want to do. What we need to do is modify that to
  • 0:33 - 0:37
    get in the inputs, and turn it into a procedure. So, to
  • 0:37 - 0:41
    make it a procedure, we need to use the def name syntax.
  • 0:41 - 0:45
    So we're going to define the procedure. Get next target. So
  • 0:45 - 0:48
    now we need our parameters, the parameters are the inputs for
  • 0:48 - 0:52
    the procedures and we decided what the input should be.
  • 0:52 - 0:54
    Is the web contents that's the string giving the contents to
  • 0:54 - 0:57
    the webpage and that's what we had in the variable
  • 0:57 - 1:01
    page. So we can call that page again here, that's what
  • 1:01 - 1:04
    we are passing in as our input and now we have
  • 1:04 - 1:06
    the body of the procedure. The code is exactly the same
  • 1:06 - 1:09
    as the code we had before. But this time, instead of page
  • 1:09 - 1:13
    being whatever it was here, page is whatever we pass in as
  • 1:13 - 1:16
    the input to the procedure. And we can change the name of
  • 1:16 - 1:18
    the page. It sort of makes sense to change that name. This
  • 1:18 - 1:21
    code works no matter what we pass in. It doesn't have to
  • 1:21 - 1:24
    be a webpage. Any string that we pass in, this code will
  • 1:24 - 1:27
    find it. So it makes more sense to give it a slightly
  • 1:27 - 1:31
    more generic name. We'll change the name to S. If we change
  • 1:31 - 1:35
    the name of the parameter, well everywhere that we use page,
  • 1:35 - 1:38
    now we don't have the value of page. What we have
  • 1:38 - 1:40
    is the value of S, which is the name of the
  • 1:40 - 1:44
    parameter. So we'll change all of those to S's as well. So
  • 1:44 - 1:47
    we're almost done. There is one big thing we have left
  • 1:47 - 1:50
    to do and that's how do we get the outputs. Right
  • 1:50 - 1:52
    we said what we wanted the outputs to be is the
  • 1:52 - 1:56
    URL and the value of end-quote. We need some way of getting
  • 1:56 - 1:59
    those back and the way that you do that
  • 1:59 - 2:02
    is to use return. Return's a special construct in
  • 2:02 - 2:05
    python. So we have the keyword return, followed by
  • 2:05 - 2:07
    a list of all the things that we want
  • 2:07 - 2:10
    to return from the procedure. So what we return
  • 2:10 - 2:14
    is a list of any number of expressions, seperated
  • 2:14 - 2:18
    by commas. We can have zero return expressions, that
  • 2:18 - 2:22
    would mean there's no output. And it actually is useful
  • 2:22 - 2:25
    to have procedures with no output sometimes. That's not true if
  • 2:25 - 2:27
    we just think of procedures as things that map inputs to
  • 2:27 - 2:30
    outputs, but procedures can also do other things. They can do
  • 2:30 - 2:33
    what we call side-effects, and what side-effects are, are things that we
  • 2:33 - 2:37
    can see, but that aren't the outputs. And an example of
  • 2:37 - 2:41
    a side-effect would be, well, if we printed something out, we would
  • 2:41 - 2:44
    see that result happen. We'd see everything that happens when the
  • 2:44 - 2:48
    code and procedure runs. But we wouldn't get it as an output.
  • 2:48 - 2:50
    So, in this case, we do want to have outputs from get
  • 2:50 - 2:54
    next target. We want to return results, so we know how to
  • 2:54 - 2:57
    continue. And we decided what the output should be, are the
  • 2:57 - 3:01
    value of the URL. So we want to find the string that's the
  • 3:01 - 3:05
    next target. And the value of the end quote, so we
  • 3:05 - 3:08
    know the position where it was found. So let's see if you
  • 3:08 - 3:10
    can figure out how to finish the return statement. And your
  • 3:10 - 3:13
    goal is to figure out the code that we need after the
  • 3:13 - 3:16
    return, to finish this procedure.
Title:
Return Statement - Intro to Computer Science
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
CS101 - Intro to Computer Science
Duration:
03:17

English subtitles

Revisions Compare revisions