-
Title:
Using Procedures - Intro to Computer Science
-
Description:
-
Unfortunately, we're not quite ready to be all smiley.
-
Sebastian tells me it's going to be a few years before I can get
-
my own self-driving car, but the bigger problem is we haven't yet talked about
-
how to actually use the procedure.
-
All we've done is make them; until we can actually use them
-
we don't have a good reason to be really happy yet.
-
We're going to learn how to use them next, then we'll be back to being smiley.
-
Now we are going to learn how to use a procedure.
-
The way to use a procedureâÂÂwe need the name of the procedure, followed by
-
a left paren, followed by a list of inputs.
-
There could be any number of inputs, but it has to match the number of inputs
-
the procedure expects.
-
These inputs are sometimes called "operands."
-
They are sometimes called "arguments."
-
We're not going to argue about that.
-
We're just going to call them inputs.
-
You have actually already done something quite similar to this.
-
Back in Unit 1, you learned about using "find" on "strings."
-
With "find" you would pass in one or two inputs.
-
The first input was a "string."
-
That was the string you are looking forâÂÂthat's the first inputâÂÂ
-
and the second input could be a numberâÂÂthe position where you start
-
looking for that string.
-
We use "find" in many ways in Unit 1, as well as you used it
-
yourself in the homework for Unit 1.
-
"Find" is a little different from the procedures that you define yourself.
-
First of all, it's built in.
-
The other thing that was different is that, instead of just having "find," we had
-
another input that was really over here.
-
We have the string that we were doing the "find" inâÂÂlet's say it was
-
in the variable pageâÂÂthat's really another input to find.
-
We'll talk in a later class about why that's done differently, but it's
-
very similar to calling a procedure where one of the inputs is over hereâÂÂ
-
the other two are here.
-
It's a little different from that and we won't get into that in this course but
-
in a later course you'll learn more about what this really means.
-
For all the procedures that you define yourself, we won't have any
-
object to invoke them on.
-
We'll just have the procedure to call and the arguments or operands or inputsâÂÂ
-
as you like to call themâÂÂto pass in.
-
Let's see how that works with a simple procedure.
-
I am going to define the procedure "rest_of_string," and we'll give it the
-
parameter "s," so that means it takes one input and we are going to use the
-
name "s" to refer to the value of that input.
-
We'll make it "return to string" from the first character to the end.
-
We will use the "string" indexing operator "return s [1:]."
-
This will evaluate to the "string," with the first letter removed, so all "strings" from
-
position 1 until the end of the "string."
-
That's what we return.
-
So, the output of "rest_of_string" is that new string that starts from
-
the second letter in the input "string."
-
Here's an example of how to use this procedure.
-
We could call it directly.
-
We could say "print rest_of_string."
-
That's our procedure.
-
Now we are going to have our paren and we are going to pass in an input.
-
There's one parameter to "rest_of_string," so we need one input
-
to pass in and it should be a "string."
-
We'll pass in the string 'audacity'.
-
What happens when we call a procedure like this, execution will jump
-
into the body of the procedure.
-
We can think of what the interpreter is doing now.
-
Instead of running the code here, the interpreter will move.
-
When we call a procedure, it will jump to run the code inside the procedure.
-
It will assign to the parameters the values passed in as the inputs.
-
We can think of this as there being an assignment that says now the value of
-
"s" is the value of this input that was passed in.
-
Now we are going to evaluate the body of the procedure.
-
In this case there's only one statementâÂÂit's this return statement.
-
We are going to find this value, so s [1:].
-
The result of that is going to be the string 'udacity'.
-
Then we got to the return.
-
What return means is we're going to jump back.
-
We're jumping back to where we called the procedure, but now we actually
-
have a result.
-
When we jump back, the value that this evaluates to is whatever value we
-
returned.
-
In this case, it's the string 'udacity.'
-
So we don't have our self driving car,
-
but now you can define and use procedures.
-
This is a really powerful concept
-
Anything that we are going to do in the rest of the course and anything
-
almost anyone does in programming computers is all about defining
-
procedures and using procedures.
-
Now we should have a big smile.
-
We can think of our procedures in terms of mapping inputs to outputs.
-
We can think of our humans as also mapping inputs to outputs.
-
We have inputs coming in through the eyes, through the
-
mouthâÂÂmaybe we even have a nose.
-
I won't try to draw any of the outputs of our human procedure, but since
-
procedures are such an important concept, we are going to have several
-
quizzes now to check that you understand them well.