[No audio]
As a last topic that we want to cover, how do I
write code that is reusable and is as compact as
possible. New developers script or any type of
software developers have a tendency to write line
by line linear code. So that you just have to go
through hundreds of lines of code to figure out
what happens. As you learn more about coding
techniques, it's very common to take any
functionality that you want to execute more than
one time and put it into a code block, so you
could just refer to it and say, go do that, go do
that again, go do that third time, without
having to have those lines of code in your program
or script over and over again. So we have two
different constructs called functions and
procedures that we use to do exactly that. So
let's go look at some Python examples on the
subtle differences between functions and
procedures, and why you might use them. Let's look
at procedures. Here we're going to go to easypythondocs.com,
and look at their entry for procedures. An easy example is we define a
procedure we use the def keyword to define a
procedure, this is called showMenu, then we just
simply list the statements we want to execute. So
in our program, when we just call showMenu, we're
just basically type showMenu. It actually
transfers control to the defined procedure, and
then runs each of the statements in it.
[No audio]
So in this example, we might want to do something to show a
menu, this is called recipeMenu, we print out
some stuff, it returns, and then we say choice is
equal input 'What do you want?' And so we asked the
user, what do you want to do. You can also pass an
argument. In this example, Example 2, we have
storyStart, and we take an argument called name.
So inside the procedure, we simply print Once upon
a time, name, was imprisoned in a castle.
They were desperate to escape, but couldn't. So
then when we call the function, we first off
assign userName as input, 'What is your name?', so
the user would type in a name, and that's
userName, then we call the procedure
storyStart, and we pass it the name userName,
so then it would output Once upon a
time, whatever we typed in for name, Michael, was
imprisoned in a castle. They were desperate to
escape, but couldn't. So that's how a procedure
works. It just simply transfers control, and runs
the statements that are define for that
procedure, and when it finishes running, it
returns to wherever you were. We can use two
arguments. We can use lists as arguments. We can
use all kinds of things such as global variables,
but that's a procedure. So the question is, how
does that differ from a function, and there's
really one subtle difference in what a function
does and how it's used. So I guess that's two
subtle differences. Let's take a look at our old
friend, portscan.py. So if we refer back to portscan.py,
we import our libraries, we set some
values, and then we say def porttry and this
looks a lot like a procedure. But it's not a
procedure, it's a function. The reason we know
it's a function is because functions return
values. So we know that porttry is going to
execute some code, but it's also going to return a
value either True or None.
So that's one subtle difference. Procedures don't
return values. Functions always return a single
value, or it by default returns a single value. If
we then go see how it's used, remember, for a
procedure, we simply type the name of the
procedure, the procedure runs, and then returns
back to where we were and goes on with the
program. In a function, we can use a function in
another statement. In this case, we're going to
say value is equal to porttry. So we're going to
run a function, the function returns a value and
we take that value and assign it to the
variable called value. So that's the difference
between functions and procedures. How do you know
which one you want to use? Well, it depends on
what you want to do. If you want to run some code,
and then get some output data to use, probably
should use a function. Functions are very useful
for returning status information, so that when you
get the function back, if everything worked well,
if the status is good, then you don't do anything,
or maybe that tells you to go do something, and if
the status is bad, you may take a different
avenue. So if you get an error return from a
function, it may say, 'Well, let's go try to do
this a different way', whereas the if you get a
positive return value, you may say, 'Okay, we got
our data. Now let's go fetch the data and use it'.
For a procedure, you don't expect to return value,
you simply go do something and then continue where
you were working from. So that is a brief
difference between functions and procedures. Use
them anytime that you try to do something more
than one time, you should put that code that
you're running more than one time into a procedure
or function at the top of your program. So in the
body of your program, you just simply refer to it when you need it.
[No audio]