4.10 Example Method

Hello welcome to this second part dedicated to methods Also call function We call. Function a set of program that allows to perform a set of a statement. by a simple call. Of the function in the body of the main program Here we have our main method. Which has a parameter and the body of the method with a instruction set to start with, we will write a method class ? New java class we will name our method. And in our class we will create several methods one method with parameters one method without parameters. Static void we call userName. It doesn't take any parameter and we'll put the name of the user. Johnny and we will create another method, but with parameter userNamePara that we call name. We will put print we will concatenate the name so the name There are the parameters. We put. In our method. Now we have. our two methods First method without parameter The second one with parameter and of course, we can have. Several parameters, we will create another method With two parameters. twoUser One parameter name We have two parameters and we will concatenate. User 01 name01 then user02 and we concatenate with name02 Then to call these methods you have to put the name of the method user name and there as you see on intellij the name changes the color of the method changes. We will compile our program and in the console, we'll see Johnny as you see him johnny then we'll call the second method. Since the second method has a parameter. We need to pass an argument so we'll create a variable. Name of user Parameter Henry And to call the second method, it's exactly the same. The name of the method userNamePara we will put in argument the name we have. Put in our variable name We compile our program. And in the console, there will be Johnny. Sorry, we'll see Henry. the first method So Johnny for the second Henry Now for the third Since we also have to pass arguments, we'll create a second variable. We'll put prince twoUser The name of the method then we'll pass the two arguments, we'll put the name Henry plus name02 We'll compile our program. In the Console. user01 Henry user02 Prince Now that you have understood how the methods work and also how to call them, we'll do a little how to call them, we'll do a little exercise and in the next lesson we'll correct the exercise The exercise consists in creating a method to calculate the final score of the user to do this forget to initialize. The final score is 0. See you soon,