Dark Horse Programmer--java Basic Syntax (c) function

Source: Internet
Author: User

-----------android training ,Java training , Java Learning Technology blog, look forward to communicating with you! ------------ 

Function

1. Definition: In Java, a function is also called a method, which is a separate applet with a specific function.

2. Format: Modifier returns value type function name (parameter type form parameter 1, parameter type parameter 2)

{

Execute the 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 formal parameter.

return: Used to end the function.

Return value: The value is returned to the caller.

3. Features of functions

A, define functions to encapsulate the function code

b, facilitates the reuse of this function

C, the function will be executed only if it is called

D, the appearance of functions improves the reusability of code

E, for a function that does not have a specific return knowing the condition, the return value type is denoted by the keyword void, then the return statement in the function can be omitted from the last line if not written.

Note: A, only functions can be called in a function and cannot be defined inside a function.

b, define the function, and the result of the function should be returned to the caller, which is handled by the caller.

4. Application of Functions

How do I define a function?

A, since the function of a separate function, then the function of the results of the operation is to be clear first. (return value type for explicit functionality)

b, and then specify whether unknown content ( variables ) should be involved in the process of defining the function. (Specify the parameter list of the function (type and number of parameters))

5. An important feature of the function--overloading (override)

Concept: In the same class, more than one function with the same name is allowed, as long as the number of argument lists or the parameter types are different.

Features: Regardless of the return value type, see only the parameter list.

Benefits: Easy to read and optimized for programming.

Example:

Returns the number of two integers and

int add (int x,int y) {return x+y;}

Returns the number of three integers and

int add (int x,int y,int z) {returnx+y+z;}

Returns two decimals and

Double Add (double x,double y) {return x+y;}

When do I use the overload?

When the functions defined are the same, but the unknown contents of the participating operations are different. Then, a function name is defined to represent the function, to facilitate reading, and to distinguish multiple functions with the same name by different parameter lists.

Practice:

Dark Horse Programmer--java Basic Syntax (c) function

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.