Function
Four aspects:
Definition of function
Features of the function
Application of the function
Overload of a function
definition and characteristics of function
1 What is a function?
A function is an independent applet defined in a class that has a specific function, also known as a method
2) Format of functions in Java:
The modifier returns the value type function name (parameter type argument 1, Parameter type form parameter 2, ...). )
{EXECUTE statement;
return value;
}
Return value type: The data type of the result after the function is run
Parameter type: is the data type of the formal parameter
Formal parameter: is a variable that stores the actual arguments passed to the function when calling the function
Actual parameter: The specific value passed to the formal parameter
Return: For End Function
Return value: This value is returned to the caller
3 features of the function
A the function code can be encapsulated by defining functions
b) Facilitate reuse of the function
c) function will be executed only if called
D) The presence of the function improves the reusability of the Code
E for cases where the function does not have a specific return value, the return value type is represented by the keyword void, and if the returned statement in the function is omitted on the last line, the system will automatically add it to you.
Note:
A the function can only be called in a function and cannot be defined inside a function.
b When a function is defined, the result of the function should be returned to the caller for processing by the caller.
C When the function operation, there is no specific return value, this is the return value type with a special keyword to identify the keyword is void,void: Represents a function does not have a specific return value.
D when the return value type of the function is void, the returns statement in the function can be omitted and not written.
4 How do I define a function?
function is actually a function, the definition function is to implement the function, through two clear to complete:
1, clear the function of the result of the operation, in fact, is in the clear return value of this function type.
2, in the implementation of the function of the process is unknown content involved in the operation, in fact, is in the clear parameter list of this function (parameter type & number of parameters).
5 function functions:
1), for defining functions.
2), used to encapsulate code to improve the reusability of code.
Note: Functions can only be called and cannot be defined.
6) Main function:
1), to ensure the independent operation of the class.
2), because it is the entrance to the program.
3), because it is being invoked by the JVM.
7) What is the function definition name?
A: 1, in order to mark the function, easy to call.
2, in order to use the name can be defined function, in order to increase the readability of the code.
Second, the application of the function
1) Two clear
A what is the final result of defining the functionality to be defined?
b Clear whether unknown content is required to participate in the operation in defining the feature
2) Example:
Example 1:
Copy Code code as follows:
Class Functiondemo
{
public static void Main (string[] args)
{
int x = 4;
System.out.println (x*3+5);
x = 6;
System.out.println (x*3+5);
int y = 4*3+5;
int z = 6*3+5;
System.out.println (y);
System.out.println (z);
}
}
The above operation was found because the code duplicated the results of the operation of different data.
To improve the reusability of the code. The code is extracted. Define this section as a separate function. Convenient and future use.
The definition of functionality in Java is embodied in the form of functions.
Clear function: Need to define function, complete an integer *3+5 operation,
1, the format of the function definition is defined first.
/*
Modifier returns the value type function name (parameter type argument 1, Parameter type form parameter 2,)
{
Execute the statement;
return value;
}
Copy Code code as follows:
Class Functiondemo
{
public static void GetResult (int num)
{
SYSTEM.OUT.PRINTLN (NUM * 3 + 5);
return;//can be omitted
}
public static void Main (string[] args)
{
GetResult (5);
}
}
The summary of this little knowledge about whether return is omitted:
When there is no specific return value after the function operation, this is the return value type identified with a special keyword.
The keyword is void. void: Represents a condition in which a function does not have a specific return value.
The return statement in a function can be omitted and not written when the returned value type of the function is void.
Example 2:
Copy Code code as follows:
Class FunctionDemo2
{
public static void Main (string[] args)
{
int sum = getsum (4,6);
System.out.println ("sum=" +sum);
sum = Getsum (2,7);
System.out.println ("sum=" +sum);
}
/* The following function defines the idea of a problem, why? Because only to complete the addition operation, as to whether or not to print operations,
That's the caller's thing, don't do it in the feature. */
public static void get (int a,int b)
{
System.out.println (A+B);
return;
}
}
How do you define a function?
1, since the function is a stand-alone function, then the function of the result of the operation of what the first clear
Because this is the return value type of the explicit function.
2, whether unknown content is required to participate in the process of defining the feature.
Because it is in the parameter list of the explicit function (the number of parameters and the type of the argument).
Copy Code code as follows:
Class FunctionDemo2
{
public static void Main (string[] args)
{
/*
int sum = getsum (4,6);
System.out.println ("sum=" +sum);
sum = Getsum (2,7);
System.out.println ("sum=" +sum);
*/
Get (4,5);
int x = Getsum (4,4);
int y = getsum (7,9);
int num = Getmax (x,y);
}
Requirements: Define a feature. Completes the 3+4 operation. and returns the result to the caller.
/*
1, the explicit function of the result: is an integer of the and.
2, there is no unknown content involved in the implementation of the function, No.
In fact, these two functions are defined in the explicit function.
1, is the return value type of the explicit function.
2, specify the parameter list of the function (the number of parameters and the type of arguments).
*/
public static int Getsum ()
{
return 3+4;
}
/*
The function of the above function, the result is fixed, without extensibility.
In order to facilitate user needs. By the user to specify Addends and be addends. In this way, functionality makes sense.
Ideas:
1, the function result is one and. The return value type is int.
2, there is unknown content to participate in the operation. There are two. The two types of unknown content are int.
*/
public static int getsum (int x,int y)
{
return x+y;
}
/*
Requirements: Determine whether two numbers are the same.
Ideas:
1, clear the result of the function: The result is: Boolean.
2, whether the function has unknown content to participate in the operation. There are, two integers.
*/
public static Boolean compare (int a,int b)
{
/*
if (a==b)
return true;
Else
return false;
*/
Return (a==b) True:false;
return a==b;
}
/*
Requirements: Define the function and compare two numbers. Gets a larger number.
*/
public static int Getmax (int a,int b)
{
/*
if (a>b)
return A;
Else
return b;
Or use the following ternary operator
*/
Return (a>b) a:b;
}
}
3) Practice:
1, defines a feature that is used to print rectangles.
2, define a function that prints the function of the 99 multiplication table.
Copy Code code as follows:
Class FunctionTest
{
public static void Main (string[] args)
{
Draw (5,6);
Printhr ();
Draw (7,9);
Printhr ();
Print99 ();
}
/*
Defines a feature that is used to print rectangles.
Ideas:
1, determine the result: no, because the direct printing. So the return value type is void
2, is there any unknown content? There are, two, because the rows and columns of the rectangle are indeterminate.
*/
public static void Draw (int row,int col)
{
for (int x=0; x<row; x + +)
{
for (int y=0; y<col; y++)
{
System.out.print ("*");
}
System.out.println ();
}
}
public static void Printhr ()
{
System.out.println ("------------------------------");
}
/*
Defines a function that prints the 99 multiplication table function.
*/
public static void Print99 ()
{
for (int x=1; x<=9; x + +)
{
for (int y=1; y<=x; y++)
{
System.out.print (y+ "*" +x+ "=" +y*x+ "T");
}
System.out.println ();
}
}
}
Third, the function of the overload (overload)
The concept of overloading:
In the same class, more than one function with the same name is allowed, as long as the number of arguments or parameter types is different.
Features of overloading:
Regardless of the return value type, look only at the argument list.
Benefits of Overloading:
Convenient to read and optimize the program design.
Overload example:
Returns the and of two integers
int add (int x,int y) {return x+y;}
Returns the and of three integers
int add (int x,int y, int z) {return x+y+z;}
Returns two decimal and
Double Add (double x,double y) {return x+y;}
When do I use heavy duty?
When the defined functionality is the same, the unknown content of the participating operation is different.
Then, a function name is defined to represent the function, to facilitate reading, and to distinguish multiple functions with the same name by a different argument list.
Overload example:
Copy Code code as follows:
Class Functionoverload
{
public static void Main (string[] args)
{
Add (4,5);
Add (4,5,6);
Print99 ();
}
public static void print99 (int num)
{
for (int x=1; x<=num; x + +)
{
for (int y=1; y<=x; y++)
{
System.out.print (y+ "*" +x+ "=" +y*x+ "T");
}
System.out.println ();
}
}
Print 99 multiplication Tables
public static void Print99 ()
{
Print99 (9);
}
Defines an addition operation that gets the sum of two integers.
public static int Add (int x,int y)
{
return x+y;
}
Defines an addition that gets the sum of three integers.
public static int Add (int x,int y,int z)
{
return Add (x,y) +z;
}
}
Practice: Distinguish whether an overload
Copy Code code as follows:
void Show (int a,char b,double c) {}
The following a,b,c,d,e,f differs from the above sentence:
A.
void Show (int x,char y,double z) {}//not, because it is the same as the original function.
B.
int show (int a,double C,char b) {}//overload because the parameter types are different. Note: Overload and return value types do not matter.
C.
void Show (int a,double C,char b) {}//overload because the parameter types are different. Note: Overload and return value types do not matter.
D.
The Boolean show (int C,char b) {}//overloaded because the number of parameters is different.
E.
void Show (double c) {}//overloaded because the number of parameters is different.
F.
Double Show (int X,char y,double z) {}//not, this function cannot exist with a given function in conjunction with a class.
How to distinguish between overloads: When the function has the same name, only the argument list is looked at. Does not matter with the return value type.