a. How do we write methods with listed parameters.
b. How do we send the arguments to the method.
c. The difference between parameters and arguments.
a. If you are writing a method that does the same exact thing, like a welcome message, then parameters are generally not needed.
b. If you're writing a method that changes, like the quadratic formula, that'll need a different a, b, and c value, then parameters are super helpful.
a. When you pass an argument, you are sending a copy of the information.
Parameters are a list of variables in a method header; it sets the type and name of a variable that your method can receive and use. They are written inside of the parentheses in the method header.
public static void myMethod(int a, String b, boolean c) {}
Arguments are the actual values that are passed into the method when it is called or invoked.
myMethod(3, "Hi", true);
Parameters define what a method will accept at run time, arguments are what you actually send to the method. Therefore, parameters are in the method header, but info sent is arguments.
1. Instead of leaving the inside of the () blank, we now put what arguments our method can receive.
2. You write the type and name of the variable.
3. Separate parameters with a comma.
4. You can now use the information sent with the variable name you gave it.
5. To send the arguments, when you call your method, you put the data inside the parentheses in the same order you wrote your parameters.
Crucial Information: If you write parameters, in order to call that method your arguments have to match your parameters. You cannot write a method with two parameters and send it one. They have to match!
Tip: You can put no parameters, one parameter, or many parameters of different types. The choice is yours.
To write methods that can receive information to be used in the method.
In the previous lesson, if you created a variable in main, you could not use it in your method.
Parameters allow us to share information gathered in one method with other methods!
Basic calculator that adds, subtract, multiplies, divides, and modulus. BUT, that uses methods for every operation and all the integers must be sent to the method via parameters.
Asks the user for three integers, send those three integers to a method called max. Find which number is the largest and print that number to the screen!
Passing Info to a method from the creators of java.
An interesting read on what is a method from Mathbits.com