5, function

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45536396/article/details/102736033

function

First, the concept of function

Completion of a particular function of a piece of code, and can be reused in multiple places.

Second, the definition syntax

Main function: public static void main (String [] args) {

// logic code

}

Function: public static void function name () {// function declaration

// code for implementing logical functions specific function completion

}

Third, define the location

Class, other functions outside, in parallel with the main function.

Fourth, call the function

Where it is needed by directly calling the function name

Syntax: function name (); // () can not be less

Fifth, the parameters of the function

1. Concept: caller of the function call functions require incoming data.

2. Classification

. A formal parameters: form data required when calling the function, local variables declared equivalent, abbreviated parameter.

It may be formed from a plurality of parameter called parameter list.

public static void function name (parameter type name 1 parameter 1, parameter type name 2 Parameter 2, ...)} {

b actual parameters: incoming call the function-specific data, for the parameter assignment. Referred to as the argument.

Note: The requirements of the actual parameters of the type, the number, order and type parameter must be the same, the number, order.

Parameter data type parameter name 1, parameter name 2, embodiment 3 of the parameter names can not be written.

3. When defining parameters

1. Log: Parameters need a user name and password, authentication code

2. Registration: required parameters user name, password, phone number, gender ...

3. The number of two and ask: type the number of required parameters

4. Solutions seeking linear equations: a, b, c of the types of data

5. withdraw money: the number of passwords required parameters money card

Sixth, the return value

Function declaration must be consistent and realize the function. Function declaration defines a return type, then the implementation of the function must be returned, and the return type of the value and return value type must be consistent.

1. Return Value Type Definition

public static type of the function return value name (parameter list) {

return Return value;

}

Return type may be any type, including eight basic types of data object type + + void

Function returns: return a return value; Representative function returns, subsequent code will not be executed.

2. For the return value of a process

a. For the type of function

1) function performs normal sequence: Only one return statement.
2) there is a branch of the function: each branch there must be a return value
3) a function of the loop: the return value should be placed outside of the loop.

b. For the function does not return a value of type void

1). All the function code is completed, the function returns automatically.
2) using a keyword that represents the function returns the return code will not be executed subsequent. return;

3. Return the result value of a function of process

And with a return the same value variable type to accept the results of the function call.

Syntax: variable name = Return Value Type function name (arguments); or a direct print function name (arguments);

Seven nested function calls

Execution flow: function call, the program will remain in the position of the function call, and then proceeds to define a function of position, while the real parameter participating sequentially assigned shape, preferentially executed inside the function code, the internal code is executed after completion of the return function to the position of function calls, and then continue down.

Here Insert Picture Description

Fifth, the benefit function

1. To improve the readability of the code, reduce the redundant code, the code is more concise.

2. improve the reusability of code can be directly used by the function name.

3. Increase code maintainability, easy to modify.

4. facilitate division, facilitate collaborative development.

1. The concept of function: a code specific function is completed, and can be used repeatedly.

2. The function definition syntax

public static return type function name (parameter list) {// declaration of the function

// achieve complete specific function code for the function

}

Function of three elements: the name of the function return value type parameter list

3. function name: comply with naming identifiers, it is best to do too literally.

4. Return value of the function:

Function declaration must be consistent and realize the function.

Returns the syntax: return the return value;

For the process returns value

1. For the return value of the function processing

a) sequence flow

b) branch process

c) recycling process

2. There is no return value of the function processing

a)

b)

For the processing of the return value of the result

The function parameters

Formal parameters:

Actual parameters:

6. function call

Function name (parameter list);

Guess you like

Origin blog.csdn.net/weixin_45536396/article/details/102736033
Recommended