/* Function (method): function: improve the reusability of function code. Requirements: Do two number of addition functions. Current problem: The following code is to do an addition function, and here the addition of the function of the code is currently no reusability. Solution: If the code of a function is to be reused, then it is possible to encapsulate the function code here, which encapsulates the function code in Java in the form of a function. function definition Format: modifier return value type function name (formal parameter ...) {The function code that needs to be encapsulated;return the result;} Parse function: Public static int add () {int a =2;int b =3;return a+b;} Modifier: public static Returns the value type: int . The return value type is the data type of the result returned after the function has finished running. Note that: some functions are returned to the caller without a result, the return value type is void. Function name: add functions: The function name to use if you need to call the function. function names as long as they conform to the naming rules for identifiers. naming conventions for function names: The first word is all lowercase, the first letter of the other words is capitalized, and the other lowercase. Formal parameter: If a function is running and there is data to be determined by the caller, the formal parameter should be defined at this time. return : returns a result to the caller. The feature of the:1. function is defined so that it needs to be called before it is executed. The main function is called by a JVM and does not need to be called manually. */class demo1{ public static void main (String[] args) { //int sum = add (2,3); //called the Add name function. &nBSP;  //SYSTEM.OUT.PRINTLN ("Result:" + sum); add (2,3) ; } /* //function of the addition function public static int add (int a,int b The { // int a,int b form parameter: The value of the formal parameter is given to the caller. return a+b; } requirements: defines a function to do the addition function, does not need to return the result to the caller , the results can be directly output. */ public static void Add (int a ,int b) { int sum = a+b;         SYSTEM.OUT.PRINTLN ("Result:" + sum); }}
/* Function definition Format: modifier return value type function name (formal parameter ...) {The function code that needs to be encapsulated;return the result;} How to define a function:1. return value type. 2. whether there is an unknown parameter (whether there is a parameter to be determined by the caller). ----> Form parameter */class demo2{ public static void main (String[] args) { //system.out.println (" Hello world! "); //int max = getmax (14,5); //called the function actual parameter //system.out.println ("Maximum value:" + max); getmax (3,7); } // Requirement 2: Define a function to compare the data size of two int types, do not need to return the maximum value to the caller and print directly. public static void getmax (int a, int b) { int max = 0; //defines a variable to hold the maximum value of the if (a>b) { max = a; }else{ max = b; } system.out.println ("Max:" + max); } /* //requirements 1: defines a function that compares the data size of two int types, returns the maximum value to the caller. public static int getmax (int a, int b) { // formal parameter int max = 0; // Defines a variable to hold the maximum value of if (a>b) { max = a; }else{ max = b; } return max;//returns the result to the caller } */}
/* Requirements 1: define a function to determine the rank of a fraction and return the rank of the score to the caller. "A-level" "B-rank" How to define a function:1. return a value type. String 2. Unknown parameters---> form parameters. Score Requirements 2: defines a function to print a multiplication table without returning any data. 1. return value type. void 2. Unknown parameters---> form parameters. what is the function of multiplication table:1. function is to encapsulate a function code to improve the reusability of functional code. The 2. function is defined so that it needs to be called before it is executed. 3. If a function does not return a value to the caller, the return value type must be represented by a void. */class demo3 { public static void main (String[] args) { //string result = getgrade (189); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//SYSTEM.OUT.PRINTLN (Result); print (7); } //requirements 2: define a function to print a multiplication table without returning any data. public static void print (Int row) { for (int i = 1 ; i<= row ; i++) { for ( int j = 1 ;j<=i ;j++ ) { system.out.print (i+ "*" +j+ "=" +i*j+ "\ T"); } // Line Wrapping System.out.println (); } } //requirements 1: defines a function that determines the rank of a fraction and returns the rank of the score to the caller. public static string getgrade (Int score) { String grade = "";//define a variable storage level if (score>=90&&score<=100) { grade = "A Grade"; }else if (score>=80&&score<=89) { grade = "B Grade"; }else if (score>=70&&score<=79) { grade = "C Grade"; }else if (Score >=60&&score<=69) { grade = "D grade"; }else if (Score>=0&&score <=59) { grade = "E-grade"; }else{ grade = "Retake Grade"; } return grade;//returns the rank to the caller }}
/* Function: function: in order to improve the reusability of functional code. function definition Format: modifier return value type variable name (formal parameter) {function body} function:1. function is to put a function of code encapsulation to achieve improved functionality code reusability. After the 2. function is well defined, it needs to be called to execute. 3. If a function does not return a value type, it is denoted with the void keyword. Requirements: Define a function to determine the rank of the score, and then return the rank corresponding to the score. "A Grade" */class demo1 { public static void main (String[] args) { string result = getgrade (90);//Call Function system.out.println ("The corresponding rank is:" + result ); } public static string Getgrade (Int score) {//unknown parameter is defined in the formal parameter, determined by the caller of the function. if (score>=90&&score<=100) { return "A Grade"; }else if (score>=80&&score<=89) { return "B Grade"; }else if (score>=70&&score<=79) { return "C Grade"; }else if (score>=60&&score<=69) { return "D grade"; }else if (score>=0 &&score<=59) { return " e-grade "; } } /* public static string getgrade (Int score) {//unknown parameter is defined in the formal parameter, is determined by the caller of the function. String grade = ""; //is used to store the corresponding level &Nbsp; if (score>=90&&score<=100) { grade = "A Grade"; }else if (score>=80&&score<=89) { grade = "B Grade"; }else if (score>=70&&score<=79) { grade = "C Grade"; }else if ( score>=60&&score<=69) { grade = "D grade"; }else if (score>=0&& score<=59) { grade = "E-grade"; } return grade; // Returns the rank of the score to the caller. } */}
/* Note: If the return value type of a function is a specific data type, then the function must ensure that the return value is guaranteed in any case. The return keyword (in addition to the return value type is void):1. returns the data to the caller of the function. Once the 2. function executes to the return keyword, the function ends immediately. (can end a function) Note: A function's return value type is void, the return keyword can also appear, but the return keyword cannot have data behind it. The break keyword differs from the return keyword: The 1.break keyword is the end of a loop. The 2. return keyword is the end of a function. */class demo2 { public static void main (String[] args) { //string result = getgrade (10);//Call function //system.out.println ("The corresponding rank is:" + result ); //add (0,2); print (); } public static void print () { for (int i = 0 ; i < 5; i++) { System.out.println ("Hello world"); ///break; //end of the current cycle return ; //ending the current function } system.out.println ("haha can I do it??"); /return cannot execute, but the compiler does not think it is nonsense } // Currently the return value type of this function is void, then can the return keyword be present? public static void add (int a , int b) { if (a==0) { return; //End a function } system.out.println ("Sum:" + (A+B)); } public static string getgrade (Int score) {//unknown parameter is defined in the formal parameter, determined by the caller of the function. &nBsp; if (score>=90&&score<=100) { return "A Grade"; }else if (score>=80& &score<=89) { return "B Grade"; }else if (score>=70&&score<=79) { return "C Grade"; }else if (score>=60&&score<=69) { return "D grade"; }else if ( score>=0&&score<=59) { return "E-Class"; }else{ return "Retake grade"; } } }
/* Requirements: define a function to do the addition function. function overloading: The same function name can have different functions to handle parameters of different numbers or different data types. Function overloading requires the:1. function name to be the same. 2. parameter list is inconsistent. (The number of formal parameters or the corresponding data type is inconsistent) the overload of the 3. function is irrelevant to the return value type of the function. */class demo3 { public static void main (String[] args) { //system.out.println ("hello world!"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//ADD1 (; ) Add (1,2.0); } // These functions are doing the addition operation. public static double add (int a, int b) { system.out.println ("Sum of two parameters: " + (a+b)); return 3.14; } //repeating definition public static int add (int a, double b) {&NBsp; system.out.println ("Sum of double arguments: " + (a+b)); return 12; } /* public static void add (int a , INT&NBSP;B&NBSP;,&NBSP;INT&NBSP;C) { system.out.println ("Sum of three parameters : "+ (a+b+c)); } public static void add (Int a , int b , int c,int d) { system.out.println ("Sum of four parameters: " + (a+b+c+d)); } */}
This article from "Small Fish Blog" blog, declined reprint!
5 Java functions