Welcome back to KnowledgeCity's course on Programming and Java. I'm Cliff Brozo I'm your instructor and in today's lesson, we're going to talk about functions. Java calls its functions methods while most other programming languages call them functions. The idea behind a method is the same. It performs a certain action inside of a block of code and it runs when we call it to run. We can pass it data and that data is known as parameters and sometimes it's known as arguments. The big benefit of creating a function is we define it once and then we can call it as many times as we want. The methods must be declared inside of the class and it's defined with the name of the method followed by parentheses. Let's examine what it looks like. You've seen this before. This is our public class car and this time we only have one variable inside. It's the color of the car. I have a function called paint car. The paint car function gets some data. It gets the color choice which is a string, and then it changes the color of the car to whatever the color choice that was passed to it. The word static here means that it belongs to the car class, but it's not an object in that class and it's a method. The name of the function is paint car and the parameters are what's inside the parentheses. The word Void here means that this function does not return value. Sometimes functions will return a value and we would put int or car there down at the bottom of the program, we declare an instance called my car and create a new car, and then in our main routine, we say let's paint my car Lime Green. The words Lime Green get passed up to the paint car function and they are stored as a string in the temporary variable color choice. That variable is then assigned to the color of the car and my car is painted. And I can call this as many times as I want. The parameters can be passed to methods and it becomes a temporary variable inside that method. These parameters are specified after the method name and you can have as many as you need, but we'll see that there's a rule for how many we should have. You'll notice that some programmers take a shortcut and instead of using something like color choice, which I used in the previous screen, they might just use the letter C. We are saying that this is a bad idea just to have a C here because I don't know what the letter C means. I can figure it out because I can see that the Set color is called Lime Green. I'm passing Lime Green and Lime Green is a string so it goes into the variable C. It's much better to have a descriptive parameter. These two functions on the left one is called a setter function because it sets the variable to a value. The other one is called a getter function because it returns the variable color from the car. We have a calling function called set color, and here's the parameter Lime Green. As I said, while you can have as many arguments as you want, the best number is zero, where we don't have to pass data to the function. We might wanna pass one or even two items and that's okay, but three or more, you have to have a really good reason to do that because it makes the function do too many things. You'll remember when we were naming variables, we talked about the good, the bad, and the ugly. The same thing holds true for naming functions. The ugly would be just to use single letters, public void M (int a) public void P (double b) public void P (int c) public void M (string d). I don't know what these things mean. When I'm reading the program, I can't tell what's being called nor can I tell what's being passed to that function. A little bit better but still bad is to give the function a name miles, price, paint, or model and that's better but still, passing individual characters as variable names. I don't know what's being passed here. The good is to have functions called getmiles, setprice, paintcar, getmodel and I pass an integer called miles, a double called price, a string called color, or a string called model. That's why these are good and those are bad and ugly. Remember a function should do only one thing and there are two rules when you create functions. Rule one is to make sure that the function is short. Rule two make sure the function is shorter than rule one. And that's a reminder to keep your functions as short as possible. Oracle has some rules for naming classes, methods, variables, and constants, and they're displayed here in this chart. In all cases, the rules effectively say the same thing. Keep the names simple and descriptive. Throughout history, programmers have had difficulty naming things. That's why your computer has a C drive. Well, there Used to be A and B drives as well. Those were terrible names to name the drives for your computer, but the creators of the PC were not very imaginative and they just came up with A, B, and C. Even programming languages started out as A. There was a language called B and certainly there's C and C plus plus. Not very imaginative at all. When we're writing code, we want to be descriptive and we want to be short. So there were the rules directly from Oracle for naming Java. Join me next time when we'll take a look at some of the built-in functions that are in Java, and we'll write a program that uses as many of them as we can squeeze in. I'll see you soon.