Return Statement – Intro to Computer Science


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

Add a Comment

Your email address will not be published. Required fields are marked *