- Preface
Before the function is introduced, all the code is written in main function, too much code, code redundancy, and poor readability. After the
introduces a function, a function is a block of code that implements a particular function. Multiple functions can be defined in a class, and each function and main main function are side-by-side relationships.
- function:
- syntax: public static Returns a value type function name (parameter list) { //function declaration part.
//function
The three elements of the
}
- function--> return value type, function name, parameter list
- return value type:
- void----means that the function finishes executing, without a return value.
- Eight basic types, reference types. --> indicates that the function must have a return value. The function must have a return XXX statement, and the XXX type must be the same as the return value type.
- function Name:--> Hump naming method: First letter all lowercase, followed by the first letter of the word capital.
- parameter list:--> when defining a function, the type, quantity, and order of the parameters
- functions are called:-> Calls to functions within the function.
- argument list: Used to specify the value of a parameter when a function is called.
- return Value: If the return value type is not void, you can return the value and use it later.
- procedure:
- jumps to the position defined by the function when the function is called;
- arguments assign values to parameters
- Execute function Body language The
- encountered a return or the end of the program, returning to the location of the function call with the return value. The
code is as follows:
|
Package com.lvsling.test;
Public class test1 {
Public staticvoid main (string[] args) {
int s1=testfunction(2, 3);
System. out. println (S1);
int s2=TestFunction2(2, 3);
System. out. println (S2);
}
Public staticint testfunction (int A,int b) {
int i=a+b;
return i;
}
Public staticint TestFunction2 (int a , int b) {
int i=a*b;
return i;
}
}
|
- Self-summary
These are the advantages of the function and the specific use of the function, pay attention to the three elements of the function can be, return value type, function name, parameter list.
Expansion and Promotion:
- The parametric name of the function is equivalent to the local variable of the function and can only be used inside the function.
- The main function is a static function, the static function can only call the static function directly, may not directly mobilize the dynamic function
- Recursive function--the > function has called its own function inside, note to avoid infinite recursive call.
The code follows the recursive invocation of the:--> function.
|
Package com.lvsling.test;
Import Java.util.Scanner;
/*
* calculates the factorial of a number
*/
Public class test2 {
Public staticvoid main (string[] args) {
System. out. println (" Enter an integer: ");
Scanner Scan = new Scanner (System. inch );
int x = Scan.nextint ();
int result = recursion(x);
System. out. println (result);
}
// Recursive Functions
Public staticint recursion (int x) {
if (x<=0) {
return 1;
}Else{
return x*recursion(x-1);
}
}
}
|
Java Essentials Note (3)----function