C ++ 2: transfer function and

C ++ 2: transfer function and

Qiang 201831061427

table of Contents 

  I. Function
  II. Function Overloading
  three value passed
  four Address transfer
  V. recursive function

A. Functions

  We often use the code is compiled in function, the function module is the basic unit of division, it helps us all code is divided into a small piece of a small piece of function, not only easy to read our developers can easily follow-up testing and modification, maintenance. Use function so that the readability, maintainability stronger. At the same time function can be called repeatedly, but also to improve the reusability of modules.
  Think about if one day you want to maintain a one million lines of code, you find open all written in the main function, you might first thing is to find writers and beat severely, long code for developers memory, logical order is a great challenge, just as our books, with sub-chapter section, if you do not get a book chapter, all the characters are together, you are in the process of reading and recitation, it should be We will be able to appreciate the importance of using the function.

II. Function Overloading

  Overloads function is a special case, for ease of use, C ++ statements to allow several functions with the same name in the function similar to the same range, but the same name as a function of these parameters form (refer to the number of parameters, the type or order) must be different , that perform different functions with the same arithmetic operator. Overloaded function code developer can easily be too long, it is difficult to find the calling function, a function of automatically match the corresponding parameters used.
For example:
1
the result is:
2

III. Passed by value

  It means the value is passed when calling function copies the actual parameters passed to a function, so if modify parameters in functions will not affect the actual parameters.
3

IV. Address delivered

  When a process is invoked, passing the arguments is the memory address of the variable parameter to the called procedure, i.e. shape memory cells involved in the argument with the same address. Therefore, when changing the value of the parameter in the called procedure, it changes the value equal to the argument.

例如
void fun(int *x){

*x += 5; //修改的是指针x指向的内存单元值

}

void main(void){

int y = 0;

fun(&y);

cout<<\"y = \"<<y<<endl; //y = 5;

}

V. recursive function

  A function in his body of the function calls himself called recursive call, perform a recursive function calls itself repeatedly, each execution time into a new layer. In order to prevent endless recursive function must have a termination condition in the function. For a function just know his style and recursive definition of boundary conditions, you can compile a recursive function.

例如:
int jiecheng(int n)
{
if(n==1) //当n等于1时,直接返回。
return 1;
else //否则,返回n*jiecheng(n-1)
return n*jiecheng(n-1);
}

Guess you like

Origin www.cnblogs.com/Drac/p/11522012.html