In simple terms of function

A function definition

     <Function Type> <function name> (formal parameters)

{

    Function content

}

Formal parameters means that these parameters do not actually exist, but in the form of parameters representative of the actual running time appears. Parameter form has the following characteristics:

1) when the function is called with a parameter, between the calling function and the callee function is implemented by data transfer parameter.

2) a function of shape parameters into the allocated memory by the system only when the function is called, the calling function the actual parameters for receiving the passed.

#include <QCoreApplication>

int max(int x,int y)
{
    return x>y?x:y;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int a = 10,b = 9;
    int res = max(a,b); // a,b为实参
    qDebug("%d",res);
    return a.exec();
}

Parameters need to comply with the rules is conventional, formal and actual parameters must be the same number, the same parameter data type and arguments, and the order parameter argument must correspond.

Second, the function parameter passing

2.1 passed by value

Passed by value, also known as traditional values, which is a parameter passed in the form of an ordinary variable, or an expression argument variable parameter assigned to the arguments.

Features passed by value after the transmission parameters, arguments and parameters no longer have any contact. Note that, at this time is the argument expression, it is impossible to arguments parameter assignment. When the function is called, the system parameter assigned to the respective storage units for receiving data transmitted arguments. During the function call, parameters, and arguments stored Danyun each independently owned. End function electrophoresis, recovery system assigned to the parameter storage unit.

Advantage of call-by-call function is no impact on its external variables, can only return a value return, a strong function of independence.

2.2 passed by reference

Passed by reference, also known as pass by reference. I.e. a reference variable alias, the alias access is access to the associated alias, or vice versa, reference character & called.

int i;
int &ai = i;// 定义int型引用ai是变量i的别名
ai = 15;//此时i的值也为15

Use references should note the following:

1) defining a reference, it should also be initialized, and it has a variable associated with the same type.

2) a reference associated with a variable;

3) is mainly used as a function of a reference parameter and return value 

#include <QCoreApplication>

int max(int &x,int &y)
{
    return x>y?x:y;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int x = 10,y = 9;
    int res = max(x,y); // x,y为实参
    qDebug("%d",res);
    return a.exec();
}

x is a medical argument, y b is a reference to the argument, they occupy the same memory cell, different knowledge names. The main features of reference are as follows:

1) Efficient transmission parameters, to avoid the copy of the configuration by value. Passed by reference are actually passed address;

2) return statement returns at most one value, and use the reference value may be modified in the form of a plurality of variable arguments; 

 2.3 manner by the pointer

And passing a pointer value is passed essentially similar, in fact, is to pass a pointer parameter to the actual parameter pointer to the function created. The difference is that, when the pointer with the information transmitted, via the parameter pointer to make the actual inter-access variables change by re-function, which is passed by value can not be done.

#include <QCoreApplication>

int max(int *x,int *y)
{
    return *x>*y?*x:*y;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int x = 10,y = 9;
    int res = max(&x,&y); // x,y为实参
    qDebug("%d",res);
    return a.exec();
}

Third, the variable storage types

Fourth, the pre-treatment

Published 73 original articles · won praise 269 · views 870 000 +

Guess you like

Origin blog.csdn.net/a8039974/article/details/104076930