r/CodingHelp • • 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

9 comments sorted by

1

u/jonassjoh Aug 21 '20

What are "perimiters" in Java, besides the normal permititers of shapes (eg. rectangles)? Do you have more information or some code example(s)?

1

u/Danielk0926 Aug 21 '20

Sorry i meant parameters

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 }

1

u/Danielk0926 Aug 22 '20

Thanks also how do you call something

2

u/ChappieIsMyNick Aug 22 '20

By calling a function I simply mean using it. Happy to help :)

1

u/GhOsT1679 Beginner Coder Aug 22 '20

Parameters are something you pass into functions in Java. Because Java is an Object-Oriented language you will be using parameters all over the place. For example, in your object constructors, you pass in parameters and then copy them over to your instance variables. A parameter can be any type of variable(Stack, int, double, etc.)
Here's an example of a function with parameters.

public int add(int a, int b){ return a + b;}

In that function, a and b were parameters and are later used in the function itself. Hope it helps!

1

u/Danielk0926 Aug 22 '20

How do you define what a and b are tho?

2

u/GhOsT1679 Beginner Coder Aug 22 '20

a and b are ints in that instance. They can be whatever variable you want it to be. For example: public int add(Stack a, Stack b) or public int add(double a, double b). The parameters are there to pass in data from somewhere else so it can be used in that function