On the function of knowledge chapter

Discuss issues function chapter for the following questions:

1: Why use function

2: Why use function overloading

3: What is the value of delivery

4: What is the address delivered

5: recursive function

A: Why use function

1: Our main function will look more simple, more clear, let's program structure is more simple.

2: The function can be called again and again, when we next want to use, do not go writing.

eg: When we calculate when a power of n

When we do not use the function, procedure is as follows:

 

 

When we use the function, procedure is as follows:

 

 It shows that, when we use the function code will be more straightforward, and when we next want to calculate the additional power calculation will be more convenient, you can call the function directly.

Two: Why use function overloading

Need to define different function name if there is no mechanism to function overloading, then the different types of data for the same operation. This will give us great inconvenience at the time of the call, and use function overloading will help us solve this problem

eg:

 

When we take overloaded functions, we can list the parameters, i.e., the number of parameters or data type may be different, the corresponding function is automatically invoked. This does not require us to write more programs that take computing.

Three: What is the value passed

In this case, the parameter is passed the value of the variable, the transmission direction is unidirectional. That is, if during the execution of the function of the parameter value changes, does not return to the argument, which is passed by value. (Because during a function call, parameters, and arguments are not the same memory cell)

eg:

 

 As it can be seen, when the value of the transfer function is performed regardless of how the parameter value change will not affect the result argument

Four: What is the address delivered

The address transfer is a way to address the parameters passed, so you can make access to the actual process variable. The result is that the process can change the real value of the variable, but its essence is passed by value. For example, there are two boxes AB, but in my case here, A and B have what you want, I put something inside you A value is passed directly, I give you the key of B box is the address transfer.

eg:

 

As can be seen from the above program, the value of this address is not passed in the argument of the function change, but the value of the address pointed to by the argument variable, in this way the value of the argument is the address of the variable.

Five: recursive function

A function calls itself recursively called in his function, and this function is called recursive functions. He repeatedly calls itself, calling each one to enter a new layer. Recursive function can be cut to some extent, our code length.

note:

   We use a recursive function when we need to know "export" in which, knowing the initial value, and find recurrence relations corresponding to good writing and call the recursive function.

eg:

We recursive function by way of example a factorial point of view:

 

 Note: To prevent recursive function to be terminated, the termination must be added the means of recursive calls within the function. Common approach is to add conditional. Although recursive functions saving significant code size, but recursive calls not only difficult to understand, but the cost is large, efficiency is not high need one level forward.

 

Guess you like

Origin www.cnblogs.com/byp-520/p/11520772.html