(upbeat music)
- In software development,
a function is just a series of finite steps
that accomplish some task.
Let's say we have the task of creating a peanut butter
and jelly sandwich.
The first step would be to gather the ingredients,
two slices of bread, peanut butter, jelly, a knife, a plate.
The next step would be to spread the peanut butter
on one slice and jelly on the other slice.
Next, you would put the two slices together
to create the sandwich
and finally you would place them on the plate.
Each of these steps that I've mentioned
is a defined step that we take in order
to complete the make PB and J task.
We call the combination of these finite steps
in association with the task of function.
So how do you use a function?
We can use it as a label or the name we give the task.
In our case,
the task name is make PB and J sandwich.
This means every time I want to make a PB and J,
we can just use the label or the name of the function,
makePBAndJSandwich instead of listing out every step.
To use the function,
we would just write the function name
with some open and close parentheses.
This makes three peanut butter and jelly sandwiches
because it executes the function code three times.
Without functions,
we would have to copy and paste all
of those steps three times
which would be unnecessarily wordy.
We want our code to be designed
as we live and work and everyday life.
When I tell someone to make a PB and J sandwich,
I don't need to tell them all the steps.
I just use the name of the task,
but at some point long ago
that person learned how to make a PB and J sandwich
and associated the steps with accomplishing that task.
For our programs,
we have to write code that defines the task
as well as all the steps needed to complete that task
or make the PB and J.
Once it's defined,
we can use the task name to execute the function
as many times as we want.
Now, our functions don't have to have a bunch of steps.
Let's say we wanted to create a function
that prints out its developer tea time
whenever it's time for tea.
The name or label for the function
would be announceDeveloperTeaTime.
And for the steps,
it would wait for the appropriate time
and then print out It's Developer Tea Time.
To use the function,
we would just need to use
the name announceDeveloperTeaTime.
So why use functions?
Functions help developers organize their code
in a meaningful way.
Organizing a series of steps under a label,
helps us keep track of certain tasks we
might want to accomplish in our program.
It also helps us define a single task once
so that it can be used all throughout our program.
For example, once we know how to make a PB and J once,
we can just use the task name to make one.
Now, what if we wanted to change how to make a PB and J
or we wanted to change how we announce developer tea time.
Instead of having to change a bunch
of separate lines in the code,
we can just go to the function steps
and modify the step we want to change.
For example, if we wanted to change our announcement
to "It's Developer Tea Time,
it's time for some snacks,"
we would just change the one print statement
in the announce developer tea time steps
instead of changing a bunch of lines of code.
Ultimately, functions make it easier to change
what a code is doing.
Next, we'll create and use our first function in Java.
To use a function in Java,
we must first define the function.
We've already seen this a little bit
with the functions we mapped out in the last lesson.
For the makePBAndJSandwich function,
we had to define the steps needed
to accomplish the given task,
make a peanut butter and jelly sandwich.
For announce developer tea time,
we had to define the steps to announce tea time.
It's important to remember that just
because we are defining a function,
does not mean we are using it.
Just because I've given you the instructions
on how to make a PB and J,
does not mean you've made a PB and J.
This means the way we write this code
will be a little different
than the things we've written in the past.
Let's try implementing our announceDeveloperTeaTime
function in Java.
To define the new set of steps that we want associated
with our announceDeveloperTeaTime function,
we'll go just above the main function
and write public static void,
and then the label of our function,
announceDeveloperTeaTime.
The name of our function is announceDeveloperTeaTime.
Now, let's follow this up with some open
and closed parentheses and open closed curly brackets.
We'll talk about the open closed parentheses
in a later lesson in this chapter,
but we are going to put the steps
for our function in between the curly brackets.
These will be the lines of code that will be executed
when our function is used.
Thinking back to the steps we laid out before,
the first step is to wait for developer tea time.
So we'll write system.out.println,
waiting for developer tea time.
To keep this as simple as possible,
we are going to make our program pause until the user types
a random word and presses enter on their keyboard.
There are other ways we could implement this waiting
for developer tea time step,
but this is the most straight forward
with what we know so far.
For this function,
once the user types of random word and presses enter,
we'll announce developer tea time.
To check if the user has entered a word into the console,
we'll need to create a scanner and use the .next operation
as we've done before.
We won't need to reference
or use the random word that the user enters
so we can write input.next and not save it to a variable.
Finally, let's announce developer tea time
with a print statement.
Awesome, now we have our first function.
The function name is announceDeveloperTeaTime
and it has five steps or five lines of code to execute.
With our function defined,
we can use it in our program.
(upbeat music)