Now let's talk about Python itself, and how you define a function and how you
going to call it. Now...
here are the particular rules, shall we say, when you start to build a function in Python,
you're going to have to give it a name, it's just quite a bit like variables, and
if you'll notice that the naming rules are pretty much the same as variables,
you can't use the keyword for a function name, for instance you can use print
because that's already built into the program. It can't contain spaces,
the first character has to be a letter or an underscore, although most
programmers don't use an underscore as the first character it just doesn't look
very very good. All the other characters they can be any letters and numbers you
wish to use or an underscore, but underscore is the only non alphanumeric
character the you can use, and in Python the uppercase and lowercase characters
are distinct and different. So if you've got a function, let's say function one, and you
write it in all capitals, and you also write a function in lowercase, those are
going to be two separate functions that can be called.
Alright, function name should be descriptive of what you wanted
to do, for instance just saying function doesn't really tell you anything, but
let's say get name would tell you what that function is supposed to be
or add numbers, so try to make the function name descriptive of what it
does, so that way it's a lot easier to find it and figure out what a function
that you're looking at it will give you a quicker look at
what that's supposed to do. Now how do you write it? In Python it's going to be DEF
it's the key word, you're gonna put a function name and then you're going to put empty
parentheses for right now, and followed by a colon. Now the colon, what it does is it
automatically gives us an in-depth for all the rest of the statements, so
that forms a block of code that belongs to that function. Ok for an example,
and this is written in the Python language, you would have DEF message, empty
parentheses, colon, and then I've got four print statements that are going to be
part of this function. Now we haven't called it, all we've done is written it,
when you run this program, what it does is it takes this function, loads it into memory,
and then it holds it there and your random access memory until that message
is called. And you're going to write the call is just the name and the empty
parentheses. Ok so what you would have is the rest of it,
now these three print lines are to the left margin, they are outside the
function, so simple on a code I would like to have our third talk, so let's
have him come down from the top and notice here what we're doing is...
...this message right in here that you're seeing is going to be the function call
the function is the name of the function, empty parentheses, now the only thing
about this is a caveat,
you have to have the function written into memory before you call it, if you try to call it before
those lines are read into memory, then of course you're going to get a
failure. Ok let's take a look at how this is going to work...
Okay let's take a look at how it's actually going to look in Python itself.
here's the Python idle, and I have two lines that are commented out
just to remind me what this program does and I have a function
right here, and it's called message, and that message is going to be the same
message from the message function, this is I'd put this print in there just
to let us know when it actually runs where these lines are coming from.
says "I am Arthur king of the Britons" now we'll go back to the line after the message
call. this is important because the message... once you've run the function, it
will return to where ever that function was called to the line just after it, so
the regular programming will then continue, and of course these print lines
here to say this is a simple line of code, these three print lines are for
outside of the function, and then this is the function call message down here,
this is the function call right here, which then has the function name, open
close parenthesis, nothing else there's no other punctuation, and of course then
we're going to return here
to three print lines, four print lines, which is outside of the function, and we're going
to call that function again a second time which we can do.
Ok let's run this and see what it looks like.
you notice is this RAM rather
quickly, because there's not a whole lot to it, but now as you can see, we have
read over here on the right side, it read the message function into memory, we
then started the program, it prints these four lines, notice that this print line
right here which, is empty
there's two different ways of doing it, this one's got two single quotes in it,
the one down below here has just empty parentheses, either way, that will print
you a blank line. So we've read that in the memory, and then we come down here,
and we've got message, it calls the function, then the computer
goes back, grabs the function activates, or runs that function, and then it
will return back to this print line here after where we called it saying
that we're outside the function, if you'll look over on the left side, you'll see
this is a simple line of code I'd like to have Arthur talk, and you can see this is
where is coming from, and then you've got the message call, here's the function "I'm
arthur king of the Britons". Now I'll go back to the line after the message call, we'll go
back to our regular programming lines, we'll call the message forward again, and
then the message runs again, you can have these functions run over and over again.
All right let's go back to our slides and see what we've got next for you.
Ok let's talk about variables you've already had a segment on variables I
hope, and so this is what are called variables inside-outside, variables as
you remember are a container in memory that hold some value, it could hold a string
of texts or it could be actual numbers. Now the variables are written inside the
function are called local variables, they exist only in that function as long as a
function is running, that variable exists in memory. That value is available for
the function to use, however it's not available to any programming lines or
outside the function. Those variables are written outside any function are called
global variables, and any function can use them.
Ok take a look at this program. This is written in Python as well, we'll try to
stay with that language, now if you'll notice here we have a variable X equals
100, that's going to be an outside variable or a global variable, then we've
got a function var X, notice I'm keeping to the naming rules, but it's telling me
it's a variable X, and it has empty parentheses, colon, and I have y equals 15
x equals five, and we're going to print x, not gonna bother with the printing Y.
And then I'm gonna call that function forward in this var X here, then
we're going to print a global X, now what I did here the reason for using
this particular example is because now you can see that the global X when I
asked it to print X, it's going to print the outside one, not the inside one,
or the global variable, not the local variable, its as defined inside the
function. Okay let's take a look at how this works.
Here's the same thing, and I'm just going to back that up.
Ok here's the same thing in the Python idle, and here's the x100, and of course
here we've got a function called def varX as I mentioned, now let's
run this and see what happens, as we run it, it's going to read this
X equals 100 into memory, it's going to read the function in the memory, we're
going to call that function, the function is going to run then it's going to
run, and it's going to put in variable X and Y into memory, it's going to then
print local X equals, this is nothing but just text said
inside double quotes, but then outside the double quotes I'm going to put a comma
X, which tells it to go find the value of x and put at the end of the string.
Let's see what it looks like.
In here as you can see, I've got the local X is equal to 5 that comes from
inside the function, and the global X is 100, and this print since it's outside the
function, it's going to print the one hundred, and now you can see that these
two x's are not the same variable. Alright let's take another look at what do we do
inside these parentheses. Ok the next thing we can do is we can pass
arguments back and forth between functions, so a local variable that's
inside a function remember I said we can't use that we can't use
that outside of the function, so the local variable is inside the function
and it only can be used by that function, well let's try something else.
Let's take that variable value, we're going to put it in the function call, we're
gonna call it a parameter, just a different name, and the format looks like
this, instead of calling this function name empty parentheses, we're going to
put that variable name inside that parameter inside the parentheses, and
then when it calls the function, it's going to pass that value to the next
function, so let's see how this works.
So what we have now is in the Python, let me get that over so you can see it, so now we've
got here we've got the program demonstrates just some comments again,
but here we're going to use main, which is the main function that I've written,
it has a value of 100, and then right here,
show double value, what we're going to do is take that value, which is a variable
name, we're going to put it in the parenthesis when we call show double, now
we're calling it before the second function is written, but one of the
things about Python and most other programming, it will grab and read all of
the functions into memory before it runs it, so if you'll notice down here the
last line, I'm calling the main function after both of those functions are read, so
I can easily tell the main function to go up here and show double, calling the
show double function, and notice that this value here, at this number here, this
value is a variable that has a value of 100, it's gonna pass that one hundred, in
the show double, I need a container to put it in, in other words I need a variable. So
in that parentheses, I then name a variable, both of these names can be the same or they can
be different like I have shown here. So what it looks like is this, so you can
see that the show double here...
...has passed 100 to the variable number in the show double function, and we're going
to use that.
So now we're going to use that to do this width, and when we're finished of
course then we're done with the program. We'll wind up running at like this. A real simple
program, but here you can see in the first function, it prints the value of
value is 100, and the number times two is out of the show double function, and of
course is past 100 number I take a number, multiply it by two, and then I come up with a result.
Okay, now let's take one last thing we're going to take a look at, let's take a
look at passing multiple parameters, and yes you can do that. We're going to take
the same one except I've added X, Y, and Z as variables in the main function, and
then when I call show double, I'm going to use X, Y, and Z separated by a comma. Now
if you'll notice that the X is then going to send that value of X to the
first variable or parameter of show double, the Y is then gonna show to
send it to the second one, and the Z is going to go to the third one, they will
always stay consistent, first one to first one, second one to second one, etcetera, and
then we'll be able to do this particular show double, I'll be able to run
that program, and we'll be able to do this mathematical expression here, and
so it winds up looking like this and we can run the program...
...and you'll see that in this case, number is equal to three hundred, which is the
variable result, and that's going to be the result here, so this variable result
is ten going to be added to my print statement. Now one of the things quickly
I want to show you was remember, when we order of precedence of numbers, if I took
these parentheses out, remember that the system all math does the
multiplication and division first, then it adds the numbers, so you'll notice if I take
the parenthesis off this, it will multiply num2 by num3,
then we'll add it to num1, so consequently we should have 2 x
50 is 100, added to 100, it should come up with a result of 200, and I
guessed right. Ok so it gives you an idea of what functions are, and so now we've
covered the advantages of using functions, what a function is, we've talked
about the syntax for defining and calling a function, we've used our local
variables and their scope, in other words how far can they be read, we
talked about global variables, and we've talked about passing arguments from one
function to the next. Hopefully this will help you
modulize your next program and thank you very much.