Parameters and Return Value
The use of global variables is avoided for the sake of clarity! So how do you get values into methods to process them there?
Parameters
Parameters form the interface to methods. Parameters allow the copying of values (or references) when the method is called. These copies are then processed by the method:

Object: Parameters are the interfaces to methods
1 public class Invoice { 2 public void valueAddedTax(double sum, int vatPercentage) // Definition of ("formal") parameters 3 { 4 System.out.println(sum * vatPercentage / 100); 5 } 6 }
1 public class Main { 2 3 public static void main(String[] args) { 4 int vatPercentage = 20; // only valid in main() 5 6 Invoice invoice = new Invoice(); 7 invoice.valueAddedTax(50, vatPercentage); // "arguments", also called "aktual" parameters 8 } 9 }
By calling the method valueAddedTax() in line 6, the arguments are copied into the local variables sum and vatPercentage: 50 into the variable sum and the content of vatPercentage from Main() into the local variable vatPercentage of the method valueAddedTax(). The number of parameters is arbitrary, but the order of the arguments must not be swapped.
The above example is in need of improvement. Outputs in the logic layer or class Invoice (line 4) have to be avoided. Otherwise the class Invoice must be changed here, if e.g. the program receives a graphical user interface (GUI).
3 Layers of programming
For this reason the VAT must be written in the UI layer and NOT in the logic layer. The scope of a variable would be limited to the method valueAddedTax(). So how do you get a value back from method to the calling method?
Return value
A method can return only one value. This return value contains either the result or an error code that can be processed in the calling method.
Object: Method returns a value
1 public class Invoice { 2 public double valueAddedTax(double sum, int vatPercentage) // Method now has the same data type as the return value 3 { 4 if (vatPercentage > 100 || vatPercentage < 0) 5 return -1; // error code = -1 indicates first error type (method is terminated) 6 if (sum < 0) 7 return -2; // error code = -2 indicates second possibilty of error 8 return sum * vatPercentage / 100; 9 } 10 }
1 public class Main { 2 3 public static void main(String[] args) { 4 int vatPercentage = 20; // only valid in main() 5 6 Invoice invoice = new Invoice(); 7 double vat = invoice.valueAddedTax(50, vatPercentage); // save return value 8 if (vat >= 0) 9 System.out.println(vat); 10 else // error reporting 11 System.out.println("Error"); 12 } 13 }
In the second program example, the result is returned (line 8) or, in the event of an error, an error code (lines 5 and 7) in class Invoice. The evaluation or output takes place in a layer above respectively in a calling method.
The definition of the method must have the same data type as the return value. (Line 2: double valueAddedTax())
In the first program example, however, there was no return value, the method was declared as follows: void valueAddedTax() (line 2, first program example). void means i.a. "Empty".
But even if a method returns no result, it still makes sense to use methods with return 0; for "no error", or -1, -2, -3 ... for different errors. This is the only way to test methods automatically.