Java development for beginners 11. Parameters and arguments. Values returned by methods.
Parameters and arguments
These two terms parameter and argument are sometimes loosely used interchangeably, and the context is used to distinguish the meaning. The term parameter, is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual value passed. To avoid confusion, it is common to view a parameter as a variable, and an argument as a value.
Parameters appear in procedure definitions; arguments appear in procedure calls. In the method definition; the variable is a parameter; in the method call the variable is the argument of the method.
In the example of the video tutorial (class Tutorial11, which is at the end of this page), we can see for example, that in the call of the method insertDataArray(array, scanner), array and scanner are the arguments and in the definition of the method ;
private static void insertDataArray(int[] array, Scanner scanner) { for (int i = 0; i < array.length; i++) { System.out.print("insert array[" + i + "]:"); array[i] = scanner.nextInt(); } }
array and scanner are the parameters.
Returning a Value from a Method
A method returns to the code that invoked it when it:
- completes all the statements in the method,
- reaches a return statement, or
- throws an exception,
whichever occurs first.
You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.
Any method declared void doesn't return a value.
If you try to return a value from a method that is declared void, you will get a compiler error.
Any method that is not declared void must contain a return statement with a corresponding return value, like this:
return returnValue;
The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.
package com.edu4java.Tutorial11; import java.util.Scanner; public class Tutorial11 { public static void main(String[] args) { int[] array = new int[5]; Scanner scanner = new Scanner(System.in); insertDataArray(array, scanner); addArray(array); int max = maxArray(array); System.out.println("Max= " + max); double average = averageArray(array); System.out.println("Average= " + average); } private static void insertDataArray(int[] array, Scanner scanner) { for (int i = 0; i < array.length; i++) { System.out.print("insert array[" + i + "]:"); array[i] = scanner.nextInt(); } } private static void addArray(int[] array) { System.out.print("Addition: "); int collector = 0; for (int i = 0; i < array.length; i++) { collector = collector + array[i]; System.out.print("+" + array[i]); } System.out.println("= " + collector); } private static int maxArray(int[] array) { System.out.print("Maximum: "); int max = 0; for (int i = 0; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; } private static double averageArray(int[] array) { double average = 0; for (int i = 0; i < array.length; i++) { average = average + array[i]; } average = average / array.length; return average; } }
<< Previous | Next >> |