z = mystery(x, z, y);
This is a call the a method called mystery, and it passes three ints to that method. A method is a short block of code that completes some function. In this case, it subtracts one from z, sets x = to 2 * y + z. and sets y = to x-1. Then it prints out the value of y and z, and returns the value of x, which is then stored in global variable z. That single line I quoted above sets all of that in action. Parameter passing was a little weird for me to understand initially, so lets try a bad analogy. Think of a method as a box. You have no idea what happens inside of the box. Let's pretend out box is called 'toaster'. Most everybody knows that if you input bread to 'toaster', it will return 'toast'. We have no idea what is happening inside, nor do we need to. Maybe your 'toaster' produces output by using heated electric coils. Maybe my 'toaster' used heated air to produce output. Our toasters are interchangeable. Either one will produce toast; we don't care how it gets done. You will hear a term called encapsulation that is used to describe self-contained blocks of code.
The other part that is confusing is that idea of scope. Scope is a term to describe what variables are visible to which code. In the main method, three ints are declared and instantiated: int x = 1, y = 2, z = 3;
. In the method, three new variables are declared and are instantiated when the method is called:public static int mystery(int z, int x, int y)
The variables declared in the main method are not the same variables declared in the mystery method. From your standpoint they look the same because they have the same letters, but to the computer, they are just memory addresses.
The variables declared in the main method can not be seen by the mystery method because the only exist inside of the main method. At the same time, the variables created in the mystery method are invisible to the main method. So when the values are passed, internally the method call looks like this:z = mystery(1, 3, 2)
Inside the method, the values are assigned to new, completely unrelated variables, where z=1, x=3, and x=2. Once mystery is done executing, the result is returned to the main method and stored in main method variable z.