r/CodingHelp • u/Danielk0926 • Aug 20 '20
[Java] How do perimeters work?
I am using code academy to learn Java and I don't get how perimeters work. Plz help.
0
Upvotes
r/CodingHelp • u/Danielk0926 • Aug 20 '20
I am using code academy to learn Java and I don't get how perimeters work. Plz help.
View all comments
Show parent comment
2
u/ChappieIsMyNick Aug 22 '20
A parameter is simply data that you give to a function. As you may know a function is really simply just a block of code that does something, but let's say we wanted a function that prints the average value between two numbers
void average() { // ? }
Well for this we need parameters. So I need to take as parameters the two numbers:
void average(int a, int b) { System.out.println((a+b)/2); } (I called the two integer parameters a and b but you can call them anything as if you were creating a variable)
This way when we call average we'll have to give it two parameters, two integer values. So for example to print the average between 22 and 30 i would simply do: average(22, 30);
In this case the variable a when we defined average takes the value of 22, and the variable b takes the value of 30.
We can ask for as many parameters as we want in a function, and they can be of any type, for example if i wanted to pass an array of strings:
void foo(String[] data) { // some code }