/* Function Parameters We'll start with an example this time. The following is an example of a function that takes two parameters and returns an integer. Specifically, it returns the largest of the two integers that are "passed" to it. class SampleClass { public int larger(int x, int y) { if (x>y) { return x; } else { return y; } } // larger } // SampleClass When a function returns a value it's giving some information back to the client that called it. A function can also receive information from the client at the time it's called in the form of parameters. In the above case, when we call the function larger we have to supply it with two integer expressions as parameters. In brush.drawLine(x0,y0,x1,y1); we supplied the drawLine function of the brush object four integer expressions as parameters. Obviously, the number and types of parameters that a function requires depends on what service, or what operation the function is supposed to provide. drawLine clearly requires the x,y coordinates of two points otherwise it won't know where to draw the line. The purpose of the "larger" function is to determine which one of two values is larger, and return the larger value. Obviously it needs to know which two values it's suppose to do its computations on. You can use this function in other parts of your program by passing it two integer values (expressions) as follows (assume as before myobject has been assigned an instance of SampleClass). public class whatever public static void main(String[] args) { int x, y; x = 4; y = x+1; SampleClass myobj; myobj = new SampleClass(); x = myobj.larger(x,y); System.out.println(x); // will print 5 } // end main } // whatever To understand functions in a broader context, you should have learned that a function maps something from its domain to its range (codomain). The domain represents the type of the function parameters and the range the type of its return value. Both the domain and the range can be the empty set. The domain of the "larger" function are pairs of integers (in mathematics, the Cartesian product of integers and integers), and the range is integers. If a function doesn't return a value, then its range is the empty set, which we write as void. A function is a self-contained little program. In fact, before we started talking about functions, your program consisted of only one function, "main". But main is in fact only the first function that is called when your program starts. As a self-contained program, any variable you declare inside a function is local to that function. For example, you can have a variable int x; declared in main and variable int x; declared in a function. Although they have the same name they are in fact different variables. This is not hard to believe if you keep in mind the concept that a function is a self-contained program in its own right. It has the right to declare variables for its own purposes, just as two completely different programs can use the variable "x" for entirely different ends. Its only connection or "interface" with other functions is by how it's called, and what values, if any, that it will return. When we define a function, we specify what parameters it requires by using declaration statements inside the parentheses, as in public int larger(int x, int y) // ... x and y are known as formal parameters. It is important to understand that x and y are local variables within the function. Every time this function is called, a new set of memory cells is allocated to hold the values of x and y. The initial values of x and y are determined when the function is called. That is, if I call myobject.larger(3,4), then in the context of this function call, x is assigned 3 and y is assigned 4. Here's another example. The following function returns the absolute value of a single integer parameter. In addition, it also declares an internal local variable. public int absval(int x) { int a; // internal variable to hold result; if (x<0) { a = -1 * x; } else { a = x; } return a; } // absval BOTH variables x and a in this function are local within this function. That is, every time absval is called new memory cells are allocated (on what's called the "runtime stack") to hold the values for these variables. a is an internal variable, while x is a parameter variable. x gets its value at the time the function is called, while a is only assigned a value inside the body of the function. Both variables effectively disappear when the function finishes executing and return to the caller. That is, you cannot refer to a or x outside of the body of the function itself. You may declare a and x in other parts of your program but they will not be the same variables! The declaration of a variable is only valid within the function (i.e. the set of braces {}) where it's defined. Let's look at a potentially confusing example: class SampleClass { public void f(int x, int y) { x = x+1; y = y-1; } // f } // sample class public class whatever { public static void main(String[] args) { int x, y; SampleClass myobj; myobj = new SampleClass(); x = 3; y = 4; myobj.f(y,x); System.out.print(x); System.out.print(y); } // main } // class whatever This function will print 3 followed by 4. The body of the function f looks like it's changing the values it received, but in fact the statements inside f have no effect on the variables x and y in main. You might also be confused by the switching of the variables names in the function call myobj.f(y,x); - On the surface it may look like that the x in f is going to be the same as the y in main, and the y in f will be the same as the x in main. This is the wrong way to think about what's happening. Remember the following principle: VARIABLES ARE NOT PASSED TO FUNCTIONS, ONLY THEIR VALUES This means that when you call myobj.f(y,x); you are NOT "giving" your variables y and x to the function f. You're only giving f the VALUES of your y and x, which in this example are 4 and 3 respectively. You are really calling myobj.f(4,3); If you think this way it will should be less confusing: myobj.f(4,3); means that the variable x inside f will receive the initial value 4 and y will receive the initial value 3. They are increased inside the function f to 5 and 4 respectively. But they do not effect the values of x and y in main, which occupy entirely different memory cells than x and y in f. Thus when I print the values of x and y back in main, they are still 3 and 4! Think this through several times. If you want the changes made in f to be reflected back in main, you must use a return statement. Quiz: what's the principle you were supposed to remember? what does it mean? Is there a way for functions to share variables at all? Yes, but they cannot be parameters or internally defined variables. They must be defined outside the function, inside the class that the function belongs. That is the subject of the next segment of the tutorial. To further illustrate functions with parameters, here's the third version of my "waiter" example. This time a function "priceof" replaces the three functions used in the previous version. */ /* Class with functions that have parameters */ class waiter { // services are defined in the form of methods (functions): public void water() { System.out.println("We don't have water!"); } public void check() { System.out.println("Here's your check. Pay up!"); } // ... other methods such as menu() omitted for brevity public double priceof(String s) { double price; // value to be returned if (s.equals("chicken")) // remember: can't use == with strings { price = 5.95; } else { // burger or fries if (s.equals("burger")) { price = 4.95; } else { price = 1.95; } } // burger or fries return price; } // priceof } // end of class waiter public class objects2 { public static void main(String[] args) { waiter ourwaiter; // declares a waiter variable ourwaiter = new waiter(); // assigns variable a waiter object double x; // variable to hold price of burger double y; // variable to hold price of chicken x = ourwaiter.priceof("burger"); y = ourwaiter.priceof("chicken"); if (x