Overview of functions
It is found that the continuous addition operation, in order to improve the reuse of the code, the function is packaged into an independent small program, the next time the need to perform the addition operation, you can directly call this small program, then the specific form of this encapsulation form is called a function.
Exercise: Add two integers:
1 public class FunctionDemo1{ 2 public static void main(String[] args){ 3 /* 4 int a = 4+5; 5 System.out.println("a="+a); 6 int b = 3+9; 7 System.out.println("b="+b); 11 public static int add(int a, int b){12 return a+b; 14 13}}Copy the code
For example, an automatic ticket machine in the subway, which receives notes or coins and returns tickets, has an independent function and can be understood as a function. There are unknown elements involved in the calculation (coins to put, paper money coins, how much). Have return value (return ticket)
Example 2: Mobile phone. The mobile phone has the call function and has unknown content (phone number). The keypad is (parameter) and the entered number is the actual parameter.
Function format
Modifiers return value type function name (parameter type form parameter 1, parameter type form parameter 2…)
{execute statement; Return Return value; }Copy the code
Return value type: The result of running this program. The result type is void if the function does not return a value. Function name: This is just an identifier and can be named at will. Formal argument: is a variable that stores the actual arguments passed in by the calling function. Actual parameter: The specific value passed to the formal parameter. Return value: returned to the caller.
Define a function:
1: Whether there is unknown content involved in the operation 2: Whether there is operation result (return value)
Example: Get the larger number of 2 integers.
1 public static int getMax(int x, int y) { 2 int result; 3 if (x > y) { 4 result= x; 5 } else { 6 result= y; 7 } 8 return result; 9}Copy the code
GetMax method
This method is called getMax, and the function of this method is to find the larger value of two integers. This method takes two int arguments, x and y, and returns the larger of the two numbers. GetMax (int x,int y) public static (int x,int y) public static (int x,int y) The code inside the curly braces is the method body return result; Result is the return value.
After the method definition is complete, how do YOU call a method?
Function call: To use a method, you must call it.
1 public static void main(String[] args) { 2 int max = getMax(5, 7); 3 System.out.println(max); 4}Copy the code
One: Call the getMax() method in main, where 5 and 7 are the actual arguments passed to the method. If the method has a return value, you can define a variable to receive the return value. The variable type is the same as the method’s return value type. In this case, the return value of the getMax method is received via the variable Max of type int. Full program here getMax(I, j); I and j are the actual parameters.
1 public class Demo6 { 2 public static void main(String[] args) { 3 int i=5; 4 int j=7; 5 int max = getMax(i, j); 6 system.out. println(I +" and "+j+" Max); 7 } 8 public static int getMax(int x, int y) { 9 int result; 10 if (x > y) { 11 result= x; 12 } else { 13 result= y; 14 } 15 return result; 17 16}}Copy the code
Two: in the above case, the getMax method is called and the result is assigned to the Max variable. You can also print the result of the getMax() method directly.
1 public class Demo6 { 2 public static void main(String[] args) { 3 int i=5; 4 int j=7; 6 System.out.println(getMax(I, j)); 7 } 8 public static int getMax(int x, int y) { 9 int result; 10 if (x > y) { 11 result= x; 12 } else { 13 result= y; 14 } 15 return result; 17 16}}Copy the code
Note: the main method is the entry of the program called by the virtual machine. Methods and methods cannot be nested. When the method finishes executing: The method ends when the return statement is executed, or when the curly braces at the end of the method are executed. This class contains two methods, the main method and the getMax method. The main method is called by the Java virtual machine and is written in a fixed way. The Main method can call other methods. When the getMax method is called, the value of the variable I is passed to x in the method, and the value of j is passed to Y in the method, and the statement in the getMax method is executed, and the return is executed, and the result of the operation is returned. The getMax method is complete.
Characteristics of functions
1. Defining functions can encapsulate functional code
2. It is easy to reuse this function
3. Functions are executed only when they are called
4. The emergence of functions improves code reusability
If the function does not return a value, the return type is void, and the return statement on the last line can be omitted.
Note:
You can only call a function from a function, not define a function from within a function.
When a function is defined, the result of the function should be returned to the caller for processing.
The return value of the function void
ABCD A90-100, B80-89, C70-79, D60-69, E0-59, double is recommended. Use the function definition for the program.
1 public static void main(String[] args) { 2 printGrade(90); 3 printGrade (59.5); 4 } 5 6 public static void printGrade(double score) { 7 char grade; 8 if (score >= 90.0) 9 system.out.println ("A"); 10 else if (score >= 80.0) 11 system.out.println ("B"); 12 else if (score >= 70.0) 13 system.out.println ("C"); 14 else if (score >= 60.0) 15 system.out.println ("D"); 16 else 17 System.out.println("E"); 18 19}Copy the code
1 public static void main(String[] args) { 2 getResult(5); 3 } 4 public static int getResult(int x){ 5 System.out.println(return x*8); 6 // An error is reported when calling this function. 7} 8 /* 9 * 7} 8 /* 9 * 7} 8 /* 9 11 times when the function is evaluated, Void void indicates that there is no specific return type. 13 * When the return type is void, the return statement in the function can be omitted. 14 */Copy the code
Note: only functions can be called from a function, not defined from within a function. Functions are horizontal and call each other.
Error writing
1 public static void main(String[] args) { 2 public static void getResult(int x) { 3 System.out.println(x * 8); 4 // Main function, inside the nested function. Error! 6 5}}Copy the code
Application of functions
Case 1: Draw a rectangle.
1 /* 2 to improve code reuse 3 define a function to draw a rectangle 4 1, determine the result of the function of the data type, void 5 2, ensure that there are no unknown parameters. 6 */ 7 public static void draw(int width , int height){ 8 for(int i = 0 ; i< height ; i++){ 9 for(int j = 0 ; j < width ; j++){ 10 System.out.print("*"); 11 } 12 System.out.println(); 14 13}}Copy the code
Case 2: Are two numbers equal when compared
1 /* 2 1, determine the data type of the result of the function operation, Boolean 3 2, determine that there are no unknown parameters. a,b 4 */ 5 public static boolean equlas(int a , int b){ 6 /** 7 if(a==b){ 8 return true; 9 }else{ 10 return false; 11 } 12 **/ 13 return a==b? true:false; 14 15}Copy the code
Case 3: Compare two numbers
4 public static int Max (int a,int b) 5 {6 /* 7 if(a>b) 8 return a; 9 else 10 return b; 11 */ 12 13 return a>b? a:b; 14}Copy the code
Note for using the function:
1. A function can only be called, but cannot be defined. A function cannot be called before it is defined. 2. Output statements can only print functions that have specific returns. 3. Functions that return a void type cannot be printed by the output statement. 4. Functions need to achieve functions, so functions only achieve the required functions, do not achieve unnecessary functions.
Function overloading
1, function overload definition: in the same class, there is more than one function with the same name, as long as the function parameter list or parameter type is different, regardless of the return value, these are collectively referred to as method overload. 2, the reason for the existence of function overload: in order to enhance the readability of the method, the program design is optimized. Case 1: The multiplication table
1 private static void print99() { 2 for(int i = 1 ; i<= 9 ; i ++){ 3 for(int j = 1 ; j<=i ; j++){ 4 System.out.print(i+"*"+j+"="+(i*j)+" "); 5 } 6 System.out.println(); 7 } 8 } 9 private static void print99(int num) { 10 for(int i = 1 ; i<= num ; i ++){ 11 for(int j = 1 ; j<=i ; j++){ 12 System.out.print(i+"*"+j+"="+(i*j)+" "); 13 } 14 System.out.println(); 16 15}}Copy the code
Exercise: Determine which method is an overload
void show(int w, double c, char b){}
Copy the code
void show(int x, char y, double z){} true
void show(int a, double c, char b){} false
void show(int a, char b){} true
void show(double c){} true
double show(int x, char y, double z){} true
Copy the code