What is a function?
A function is an independent applet defined in a class with specific functions. (The smallest package in Java)
Functions in Java are also called methods.
Function Format:
Modifier .. return value type function name (parameter type form parameter 1, parameter type form parameter 2 ,){
Execute the statement;
Return return value; // do not simply print the returned value.
}
Return Value Type: the data type of the result after the function is run.
Parameter type: it is the data type of a formal parameter.
Form parameter: a variable used to store the actual parameters passed to the function when a function is called.
Actual parameter: the specific value passed to the form parameter.
Return: Used to end a function.
Return Value: this value is returned to the caller.
Features:
The defined function can encapsulate the function code. The smallest Java encapsulation body is the function.
This feature is easy to reuse.
The function is executed only when it is called.
The emergence of functions improves code reuse.
If the function does not return a specific value, the return value type is represented by the key word void. If the return statement in the function is omitted in the last line, it is automatically added if it is not written. If the function does not return a specific value, the return value type is represented by the key word void. If the return statement in the function is omitted in the last line, it is automatically added if it is not written.
Note:
Only functions can be called. functions cannot be defined within a function.
When defining a function, the function result should be returned to the caller for processing. This is a good design method.
Public ClassFunction_define1 {Public Static VoidMain (string [] ARGs ){IntX = getresult (3); System. Out. println ("X =" +X );}//It must be modified using static because it is called by the main function. Variable name and function name: when multiple words are made up, the first letter of the first word is lowercase, and the second word starts with the first letter of each word. Public Static IntGetresult (IntNum ){ReturnNum * 3 + 5;}}
/*When there is no specific return value after function operation, this is the return value type. A special keyword is used to identify this keyword as void, void indicates that no specific return value exists. When the return value type of the function is void, the Return Statement in the function can be omitted without class functiondemo {public static void getresult (INT num) {system. out. println (Num * 3 + 5);} public static void main (string [] ARGs) {getresult (3 );}}*/
Function Definition 1