Functions in Java

Source: Internet
Author: User

1. Function: Defines a separate applet in the class that has a specific function.
Functions are also called methods.
In order to improve the reusability of code, the code is extracted.
This section is defined as a separate function. Easy to use.
The definition of functionality in Java is implemented through functions.
2 format of function: Modifier returns value type function name (parameter type form parameter 1, parameter type parameter 2,)
{EXECUTE statement;
return value;
}
Return value type: The data type of the result after the function is run.
Parameter type: The data type of the formal parameter.
Formal parameter: is a variable that stores the actual arguments passed to the function when the function is called.
Actual parameter: The specific value passed to the actual parameter.
return: Used to end the function.
Return value: The value returned by the caller.
3. After the function operation, when there is no specific return value, the return value type is identified by a special keyword.
The keyword is void. void represents a function that does not have a specific return value type.
When the return value type of a function is void, the return statement in the function can be omitted from the write.
A function can only be called in another function and cannot be defined in another function.
The smallest functional unit in 4.java is a function
How do I define a function?
1. function is a separate function, the function of the operation results are first clear, the return value type is explicit.
2. Whether the process of defining the function requires unknown content to participate in the operation, clear the parameter list.
For example: x+y; Demand analysis:
①. The function result is a and, the return type is int.
②. There is an unknown type of participation operation, and two types are int.
5. Return after return value type void cannot be output. Returns a result that cannot be called. (You can only print directly in the function.) )
The result returned by return can be called.
6. The function can be used when it needs to be called and not executed if it is not required.
7. Function overloading: In the same class, one or more functions with the same name are allowed, as long as the number of arguments or the type of parameter are different;
Overloading is only related to the function type (parameter type), and the return value type does not matter. is related to the type precedence (the order differs from the overload).
When to use overloading: When the definition function is the same, but the unknown content of the participating operation is different.
Then, define a function name to represent the function, easy to read, and through the different parameter list to distinguish multiple functions with the same name.

Can be lowered in the function with another function of the same name;

public class FunctionTest {


public static void Main (string[] args) {
Int[] Array1 = {12, 8, 4, 6, 9, 99};
Double[] Array2 = {12.0, 215.10, 6546.2, 563.12};
int max1 = Maxarray (array1);
Double max2 = Maxarray (array2);//Allow function names to be the same, call parameter types differently in a class;
SYSTEM.OUT.PRINTLN ("Maximum value is" + max1);
SYSTEM.OUT.PRINTLN ("Maximum value is" + max2);
}


public static int Maxarray (int[] array1) {
int max = 0; Looking for data in the form of a corner mark;
for (int i = 0; i < array1.length; i++) {
if (Array1[max] < array1[i]) {
max = i;
}
}
return Array1[max];
}


public static double Maxarray (double[] array2) {
Double max = array2[0]; Looking for data in the form of a corner mark;
for (int i = 0; i < array2.length; i++) {
if (Max < array2[i]) {
max = Array2[i];
}
}
return Max;
}
}

Functions in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.