>> So let's try using some functions inside our code.
If we take a look at here,
I've got that simple code we were doing in the explanations,
where I've copied and pasted three lines of code.
So I have display a first_name and then
I'm printing a statement because I want to
see how long my code is taking to run,
and I want to check the timestamp
at different points in my program,
and then after this command completes,
I also want to print out the current date and time.
So this code runs just fine,
and we'll execute that,
so I can show you the output that comes back.
Python print_time with repeated code.
So when I run that code,
you can see that it is successfully,
and I'll just move this up so you can see the full output.
So you can see it's splaying tasks completed,
current date and time, and all the outputs.
All right. That said,
it just I copied and pasted code.
So since it's the same code over and over again,
I can just take that code, move it up here,
and add a function called print_time,
don't forget a colon at the end of your function definition,
and that def means define the function called print_time,
and just paste my code here.
I do need to make sure that's indented over four spaces.
All right, and now everywhere I was
calling those three lines of code,
I can simply call the print_time function.
So here and down here,
and I should see exactly the same output except now,
I'm not copying and pasting code.
So I've just added the function call now,
and every time you call that function,
these three lines of code will be executed. So we'll save that.
We'll go down to our terminal here, rerun our code,
and sure enough, you can see,
let me just expand that again,
so you can see once again we're
seeing exactly the same output splaying.
So I'm seeing the task completed,
then it's running through my loop,
and displaying the task completed
and the current date and time again.
Now, this has the advantage,
I should definitely go and add a comment here.
This is a function to print current date and time.
So I always want to make sure I add comments with my functions.
Now the nice thing about this is if I go, "Hey, wait a second.
I learned a better way to call datetime.
I don't like this datetime.datetime.now,
that always confuses programmers.''
So I'm going to improve the way I access that library,
said and we're going to learn about
libraries in an upcoming module.
Then I can just say datetime.now there,
that's cleaner code easier for somebody else to read.
So once I learn a little more about Python,
I can make my code better written.
But I only had to change it in one place,
because now of course,
anytime I call the print_time function,
said it's already there.
Now, what if I decide,
"Hey, I want to display custom messages? "
So a different message depending on what task just completed.
What I can also do is I can add parameters to print_time.
So I can say, "Hey,
every time you call print_time,
you tell me which message you want displayed."
Right now it always displays the message task completed.
So I might say tell me the task name that you want displayed.
Then every time that you do
the print statement it says whatever task name you pass into me,
that's the message I'm going to print on the screen.
Now if I do that,
I have to make sure that every
time I call the function, I give it a value.
So if this is a printed first name,
that's the task it just completed, and here,
this is completed for loop.
So when you put parameters of a function,
you have to pass in values for it.
We'll save that, and we'll run,
and what we'll see this time is that
the message varies each time it's called.
So I see, when it printed the first name,
there's the call my message via
requested and the current date and time,
then my loop executes,
and we call the print_time function with the message
complete the for loop and the datetime.
So that gives us
more flexibility in more ways of using our function.
So just because something's a little bit different,
you might be able to use a function with parameters.
Now the other thing we do sometimes
is we might have more complicated code.
For example, just going to bring up a piece of code here to do,
let's take a look at this get_initials program.
So what this one does,
and I'll just collapse this paints,
we have focused on the code,
is we ask somebody to enter again their first name,
their middle name, and their last name,
and from that, we extract the first letter from each name.
So we take the first name that they enter,
and we go to the character position zero,
get the first letter, then we do
the same thing with the middle name and last name,
and from that we can return their initials.
So if I run that code,
then you can see how that works.
Oops, I forgot to enter file extension,.py.
My first name is Susan, middle name, Jane,
last name, Ibach, and it comes back and says,
my initials are SJI.
So this code works fine, but again,
I've got almost exactly the same logic
happening over and over again.
So there's another opportunity to bore me more efficient
and readable in our code by using functions.
So I'm going to go up here and I'm going to find
a new function called get_initial.
When you call it, you pass in a name of some sort,
and what I will do is I will get an initial back,
and the initial equals whatever the name as you passed in,
and go from character zero,
which is the first letter, and retrieve one character.
But what I'm going to do is a little
different here is, in this case,
I actually need to pass a value
back to the person that called the function.
So I say return the initial.
There we go. So now I've returned the initial.
So what I can do now is,
everywhere I was trying to extract it,
I can simply say,
"Whenever I need an initial,
call the get_initial function and pass in
the name whose initial I want to extract."
So everywhere I was doing that logic I
replace with a function called get_initial,
middle_name, there we go,
and here we go with this one here,
I'm going to change just to get_initial last_name.
Save that. When I execute the code,
should work exactly the same way.
To my first_name, middle_name,
last name, and comes back with the initials SJI.
So what did I really gain from doing this?
If you take a look at the code,
I think it's a little more intuitive to
see that calling get_initial makes it clear,
I'm retrieving an initial back.
Up here I should add some logic comment here says,
"This function will return the first initial of the name."
But what's neat about this is I can do things like go, "Oh, wait,
I've had that problem with the initials returning in lowercase. "
If I need to fix that,
then all I need to do is go to my function,
make the fix in one place, say,
"When we return initial, let's make that uppercase," and suddenly,
that code is fixed everywhere that I was calling it.
So now, first name,
middle name, last name,
it doesn't matter, all of them returned the initial in uppercase.
So that's to me the real magic of
functions makes the code a lot easier to maintain.
Now, there are some other neat things we can do with parameters,
and I'll show you that in the next module.