First, in the function definition
public static void change(int x[]){
x
is a formal parameter - you may choose arbitrary name instead of x
, as it is just placeholder for the real
parameter.
Subsequent commands show what the function will perform with real
parameter (so far unknown) when the function will be called by using formal parameter instead of real one.
Now the function is called (used):
change(lucas);
with the real
parameter - yes we want change lucas
! So the statements in the change()
function definition are now performed with lucas
instead of x
.
Second: Why we need the loop
for(int counter=0;counter<x.length;counter++){
x[counter]+=5;
}
In your particular code we don't need the loop, nor the function change()
. We may use the direct way to add 5
to every element in the array:
lucas[0] += 5 // is it shortened form for lucas[0] = lucas[0] + 5
lucas[1] += 5
lucas[2] += 5
lucas[3] += 5
lucas[4] += 5
But we may meet generally 2 problems:
lucas
may have many elements, not only 5
.
We want to do the same thing (adding 5 to each element) with other (so far not known) array, e. g. mathew
- in future.
So we will solve both of these 2 problems generally - by defining the function change()
which doesn´t know in advance the name of an array, nor the number of its items
, nor the values of its items
. Let x
be the placeholder (formal parameter) for that array.
x.length
means "use the attribute (property) length" of that array - every array has this attribute an it is the number of its items.
So, after substituting the real parameter (lucas
or mathew
) for the formal one (x
) we will know the numbers of items of this real array.
We use it in the for
loop:
for (int counter = 0; counter < x.length; counter++) {
Here is another placeholder (counter) and again - its name is not important, it may be other one, e. g. i
:
for (int i = 0; i < x.length; i++) {
We need this placeholder for indices (one index at a time) of array's element - they are numbered not from 1
but from 0
, so the 1st element has index 0
, 2nd 1
, etc. - so the last one will have index x.length - 1
.
How to reach that this placeholder (counter
, i
, or other arbitrary name) will sequentially have values 0
, 1
, 2
, etc.?
The for
statement
for (int counter = 0; counter < x.length; counter++) {
has 3 parts
in its header, delimited by ;
:
The 1st part defines what to do initially:
int counter = 0; // set the first value of the placeholder to 0
The 2nd part defines the condition for continuation (of looping):
counter < x.length; // is the placeholder's value still less than number of items?
If this condition is not satisfied, the game for for
loop is over.
The last part defines what to do (again and again):
counter++ // increase the counter's (placeholder) value
after performing commands in the body of the loop (between curly braces {}
)
Now, in the body of the loop is just 1 statement
x[counter] += 5; // Shorter form for x[counter] = x[counter] + 5;
which will be performed again and again by the previous rules (of course, after substituting the real parameter (lucas
) for the formal one (x
)).