C ++ second job function summary

The second job C ++ function summary

Function summary

1. Why use functions?

Typically relatively independent, functional abstraction frequently used as a function.
After the function completion, it can be reused, can only care about the function and use of functions when used without having to worry about specific implementation of the function does. This facilitates code reuse, can improve development efficiency, and enhance the reliability of the program, but also easy to maintain and modify the division of labor.

类型标识符 函数名(形式参数表)
{
语法序列
}

For example: a preparation of the n-th power function evaluates X

double power(double x,int n){
double val=1.0;
while(n--)
val*=x;
return val;
}

2. Why use function overloading?

First, the definition

Two or more functions, with the same function name, but a different number or type parameter, the compiler according to the best match arguments and number and type of parameters, the process of automatically determining which of a function call.

Reasons to use function overloading

If there is no overloading, then perform the same operation on different types of data but also the name of a completely different function definitions.

Second, the application

(1)
int add(int x,int y);

`float add(float x,float)`

The number of different parameter

(2)int add(int x,int y);

int add(int x,int y,int y);

Different numbers of arguments

3. What is the value of delivery

First, the definition

Refers to the value passed when a function call occurs, the parameter to allocate memory space, and to initialize a parameter arguments (argument values ​​directly transmitted to the parameter). This is the value of a one-way transfer process, the parameter value will not affect the argument.

Second, the application

void swap(int a,int b)
{
int t=a;
a=b;
b=t;
}

Analysis: Since the value transfer is a one-way transfer process, the above function and does not achieve its purpose of exchange. Parameter reference value can not be changed on real role.

4. What is the address delivered

First, the definition

By passing the address: When calling a certain process, the memory address is to pass arguments to a variable parameter of the called procedure, that is to say of actual and parameter memory unit with the same address. Therefore, when changing the value of the parameter in the called procedure, it changes the value equal to the argument.

Second, the application

void Change(int* n)
{
    *n = *n + 1;
}

Addresses will be given parameter argument, change the value of the parameter to change the value of the argument.

5. How to write a recursive function

First, the definition

Simply put, a recursive function is a direct or indirect caller of the function itself.

Second, the application

(1) Fibonacci number

int Fun(int n)
{
if(n < 1) 
{
return 0;
}
else if(n == 1 || n == 2)
 {
return 1;
}
else
{
return f1(n-1) + f1(n-2);
}
}

Analysis: The above is the typical function of a recursive function calls itself. Each equal to the sum of the first two

(2) Find the n! .

void jiecheng(int n)
{
if(n==0) 
return 1;
else 
return jiecheng(n-1)*n;
}

to sum up

Recursive process has the following two stages.

The first stage: recursive

The original problem is constantly broken down into new sub-problems, gradually advancing from the known to the unknown, and ultimately achieve a condition known that recursive end condition, this time ending recursive stage.

Phase II: Return

Starting from the known conditions, in accordance with the recursive process of you, one by one seeking the return value, and finally reached at the beginning of recurrence, the end of the return phase, complete recursive call.

note

Different times call the same function, the compiler allocates different parameter space for local variables and functions, they affect each other

Guess you like

Origin www.cnblogs.com/nianshaomingtu/p/11519635.html
Recommended