Good programmers to share study notes: Functions

First, the function

A code having a specific function, the function code is simplified, improving the reusability of code, increases the safety of the operation is simplified. Which includes a modifier, return type, function name (parameter list, {body of the function, the code execution,

return a return value, and the like}. Specifically:

Modifier: determining a current scope of function, generally employed is public (public), where said plurality temporarily.

Return Value Type: indicating the current type of return value of the function, the return value may not exist, but must be given , not even the desired void, if present, to write a value corresponding to the return type.

Function name: Name of the current function, can not be omitted, as a naming rule, subject to a small hump principle requires (a function name may consist of more than one word, except the first word, all words capitalized.) Note that the function name as far as possible see to know the name of Italy.

Parameter list: Incoming data function to execute the function, there may be zero or more, to be spaced between a plurality of parameters, no parameters, the parentheses after the function name can not be omitted.

Function body: the real function of place.

Executing code: achieve the function.

return Return Value: return can be understood as a verb, meaning refers thrown outward, the data will be thrown position return type, this value is outside the reception again. Need to prompt return of features, including:

( 1) value can be out of a routine.

( 2) end has a return means, the latter can not write code.

( 3) If the function has a return value, to ensure that in any case has a return value.

important point:

( 1) If there is no return value, you do not need to write return. If there is return, behind only with a value.       

( 2) as well as the end of the return means, the code will not be executed later.

Therefore, we create a function of time, requires that each function as much as possible to complete a function, emphasizing the unity of function.

Second, the function is called

METHODS: The name of the function + ()

important point:

1. The function call can be nested, defined functions can not be nested.

2. We defined functions must be implemented by calling.

3. The process for the called function, called object-oriented methods.

Third, the parameter argument

    Argument means the actual parameters. Parameter means a parameter form, it must play the role through the receiving argument. Application of argument, parameter should pay attention problems, their number, location, type, must be consistent, the assignment process in the actual value of the program will be paid sequentially argument parameter.

Examples of functions: a function of selecting the maximum value using the two numbers. analyse as below:

1. first to write a no-argument function that returns no value.

2. The maximum number of functionally directly thrown into two functions.

3. function to be optimized.

Fourth, global and local variables

    Global variables: general we have defined directly in the class variable called global variables.

Local variables: We define a function to obtain an internal variable for, if the parameter and the like are called local variables.

Note that only the definition of global variables and functions defined in a class that we can directly get inside to see. From the beginning of the process variables define the current variable to be destroyed for the entire life cycle. Which scope is that the life cycle of the variable range can function. We in turn broken down into its two parts scoped global variables scope and local variables.

Scope of global variables include the entire class, except modified static method. The scope of local variables is included from the beginning to define the current variable function where he, for, IF, etc. ends. When the end of the life cycle of a variable means that the variable is released. The difference is the role of different ranges, different from the default value of global variables if there is no assignment, the system will default value - 0, local variables must be assigned before use.

Fifth, the reload

For the same class of function names, function the same as the parameters are not the same, we will their relationship is called overloading. Here Why can the same name? Because the real recognition function within the program , identifying the type of the function name + all parameters. But it is worth noting that the function is essentially different functions are overloaded.

We may wish to make a summary , the function name must be the same. Parameters must be different (type, number, location), but regardless of the name of the parameter. The return value has nothing to do with the heavy load.

Guess you like

Origin www.cnblogs.com/gcghcxy/p/10984832.html