Parameters




Parameters are values that we can pass (send) to a method (function). The method will create (set up) variables that then will receive the values that are sent to the method.

Examples:



Many methods allow us to pass (send) values (data) to them.  The method will create
a temporary variable to hold the value.

Example 1:
// the call to Math.abs() below passes (sends) the value of -7 to the method // we say that we call or invoke the method // we say that we pass or send values to the method System.out.println(Math.abs(-7)); | | | | \/ public int abs(int x) // method abs in the Math class { // creates a storage location called x to hold the value of the parameter // x receives the value of -7 // method instructions if (x < 0) return -1*x; return x; // this method will return the absolute value of the parameter } Example 2:
System.out.println(Math.max(-3, 5)); | | | | | | | | \/ \/ public int max(int a, int b) // method max in the Math class { // creates storage locations called a and b to hold the value of the parameters // a receives the value of -3 // b receives the value of 5 // method instructions if (a > b) return a; return b; // this method will return the value of the bigger number } Example 3:
System.out.println(Math.max(8, 4)); | | | | | | | | \/ \/ public int max(int a, int b) // method max in the Math class { // creates storage locations called a and b to hold the value of the parameters // a receives the value of 8 // b receives the value of 4 // method instructions if (a > b) return a; return b; // this method will return the value of the bigger number } Example 4:
int x = 17; int y = 23; System.out.println(Math.max(x, y)); | | | | | | | | \/ \/ public int max(int a, int b) // method max in the Math class { // creates storage locations called a and b to hold the value of the parameters // a receives the value of 17 // b receives the value of 23 // method instructions if (a > b) return a; return b; // this method will return the value of the bigger number }