Function, the concept: ====================== 1. Let's start with something that we are already familiar with, i.e., basic functions in mathematics: A. Function definition, e.g.,: f(x, y) = x + y + xy + 1 Note: - f is the name of this function - x and y are parameters - x + y + xy + 1 is the function body - we assume that the values that will be used with this function are numbers B. Function application: Once we define a function, e.g., f above, we can call it, e.g., like this: f(1, 2) = 1 + 2 + 1*2 + 1 = 6 Note: - this is how you 'call'/'invoke'/'apply'/'use' a function - 1 and 2 are called 'actual arguments' - x and y are called 'formal parameters' - If you invoke f like this: f(11.2, 20.34), then intuitively (or based on what we are already familiar with in math) it would work because 11.2 and 20.34 are numbers and numbers can be added or multiplied which is what the body of f does. This observation may not be true depending on how we actually define f as we will see when we talk about C or Java functions below. Further note that: - since f is defined with two 'parameters', we must 'pass' in two 'values' when we call f - first actual argument (1 here) is going to be 'bound' to the first formal parameter of f (x here), and the second actual argument (2 here) is going to be bound to the second formal parameter (2 here) - The 'number' of actual arguments and the number of formal parameters must be the same, 2 in this case. - the 'type' of an actual argument and that of its corresponding formal parameter must match. In this example, we did not specify the types, so we won't worry about it until we see a C or Java version below. - Most important: We define a function ONCE, but can call/invoke it as MANY TIMES as we want, e.g., we can use f many times like this: f(1, 2) f(2, 3) f(34, 432) ... - It is a 2-step process: define a function and call the function. - x and y are dummy names and they can be renamed consistently without changing the meaning of the funtion, e.g., x to a and y to b. 2. Let's turn this into a piece of pseudo-C code with type information added. It would look like this: A. Function definition with type information added: int f(int x, int y) = return (x + y + xy + 1) Note: - 'int' in front of the function name f here means that the function would 'return' (produce) an integer when it is called/invoked/applied/used. - the second and the third 'int's mean that actual arguments used for x and y must be integers when f is invoked. - 'return (x + y + xy + 1)' means that the value computed is being returned as the value of the function call, i.e., as the value of the called function. In this case the value computed will be of type integer whose magnitude will depend on the values of x and y and that type would match the return type declared, which is 'int'. Think about how a function call to f would be used. It would for example be used like this: f(1, 2) + f(23, 21) * f(34, 22). For this sort of usage to make sense, each call to f must produce A NUMBER (A VALUE) and the return statement inside the function f is producing that kind of value. B. Function application: f(1, 2) = 1 + 2 + 1*2 + 1 = 6 If you invoke f like this: f(11.2, 20.34) then it would not work in this case because here we are passing real (floating point) numbers when integers are expected as actual arguments by f, i.e., that is how f is defined in A above. 3. Let's turn this into legal C code: int f(int x, int y) { return (x + y + x * y + 1); } int main() { int val = f(1, 2); printf(val); return val; } Note: - main calls f in the body of main. In this example, we call main the 'caller' and f the 'callee'. - Here, . function f is defined and applied correctly . the name of function, f, is spelled correctly . types of the actual arguments and those of the formal parameters match, one-to-one . the number of arguments and the number of parameters match . the value returned by f is an int and it is used as an int in main after it was returned by f. 4. Let's turn this into legal Java code: public class FunctionExample { public static int f(int x, int y) { return (x + y + x * y + 1); } public static void main(String[] args) { int val = f(1, 2); System.out.println(val); } } This gives a quick tour of defining and using a function/method in Java from the concept of a function.