Introducing Methods a method is a collection of statements that are grouped together to perform an operation Define a method Invoke a method modifier return value type method name formal parameters method k header public static int max (int numl, int num2)( int z= max(x, y) int resulti method actual parameters if(numl> num2 parameter list (arguments) result return value result return result Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Introducing Methods A method is a collection of statements that are grouped together to perform an operation. public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } modifier return value type method name formal parameters return value method body method header parameter list Define a method Invoke a method int z = max(x, y); actual parameters (arguments)
Introducing Methods, cont Method signature is the combination of the method name and the parameter list The variables defined in the method header are known as formal parameters .When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Introducing Methods, cont. •Method signature is the combination of the method name and the parameter list. •The variables defined in the method header are known as formal parameters. •When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument
Introducing Methods, cont a method may return a value. The return Value Type is the data type of the value the method returns If the method does not return a value. the return value Type is the keyword yoid. the return valueType in the main method is void public static void main( String args[]( Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Introducing Methods, cont. •A method may return a value. The returnValueType is the data type of the value the method returns. • If the method does not return a value, the returnValueType is the keyword void. the returnValueType in the main method is void. public static void main( String args[ ] { }
Calling Methods Example 4. 1 Testing the max method This program demonstrates calling a method max to return the largest of the int values Testmax RI un Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Calling Methods Example 4.1 Testing the max method This program demonstrates calling a method max to return the largest of the int values TestMax Run
Calling Methods, cont the value of pass the value of j oublic static void main(st args) i public static int max(int numl, int num2) Int int k= max (1, )i if (numl num2 result System. out. println( else The maximum between h+i+ result num and+ j Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Calling Methods, cont. public static void main(String[] args) { int i = 5; int j = 2; int k = max(i, j); System.out.println( "The maximum between " + i + " and " + j + " is " + k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } pass the value of i pass the value of j