A function is uniquely represented by a name and a set of operand types. Its operands, referred to as parameters, are specified in a comma-separated list enclosed in parentheses. The actions that the function performs are specified in a block,
referred to as the function body. Every function has an associated return type.
As an example, we could write the following function to find the greatest common divisor of two ints:
-
- int gcd(int v1, int v2)
- {
- while (v2) {
- int temp = v2;
- v2 = v1 % v2;
- v1 = temp;
- }
- return v1;
- }
Here we define a function named gcd that returns an int and has two int parameters.
To call gcd, we must supply two int values and we get an int in return.
调用与被调用的函数也称为caller 和callee。
这段话准确地表述了函数调用的过程:先初始化函数参数,再进入被调用的函数体执行其中的语句。“初始化参数”这一步所涉及的副作用会在开始执行被调用函数之前完成。例如,在foo(bar()) 这个语句中,会先执行bar(),用它的返回值来初始化foo() 的参数,再执行foo()。如果函数参数不止一个,那么参数的初始化顺序是未指明的(unspecified)。一般应该避免写出foo(g1(), g2()) 这种调用,因为不能确定先调用 g1 ()还是g2()。同理,应该避免函数实参的表达式具有副作用,例如foo(i++, i+1) 中的两个表达式的求值顺序是未指明的。当然,这里
有一个例外,如果g1() 和g2() 都是不改变程序状态的纯函数,那么程序还是安全的。
Calling a Function
To invoke a function we use the call operator, which is a pair of parentheses. As with any operator, the call operator takes operands and yields a result. The operands to the call operator are the name of the function and a (possibly empty) comma-separated list of arguments. The result type of a call is the return type of the called function, and the result itself is the value returned by the function:
-
- cout << "Enter two values: \n";
- int i, j;
- cin >> i >> j;
-
-
- cout << "gcd: " << gcd(i, j) << endl;
If we gave this program 15 and 123 as input, the output would be 3.
Calling a function does two things: It initializes the function parameters from the corresponding arguments and transfers control to the function being invoked. Execution of the calling function is suspended and execution of the called function
begins. Execution of a function begins with the (implicit) definition and initialization of its parameters. That is, when we invoke gcd, the first thing that happens is that variables of type int named v1 and v2 are created. These variables are
initialized with the values passed in the call to gcd. In this case, v1 is initialized by the value of i and v2 by the value of j.
Function Body Is a Scope
The body of a function is a statement block, which defines the function’s operation. As usual, the block is enclosed by a pair of curly braces and hence forms a new scope. As with any block, the body of a function can define variables. Names defined inside a function body are accessible only within the function itself. Such variables are referred to as local variables. They are “local” to that function; their names are visible only in the scope of the function. They exist only while the function is executing. Section 7.5 (p. 254) covers local variables in more detail.
Execution completes when a return statement is encountered. When the called function finishes, it yields as its result the value specified in the return statement. After the return is executed, the suspended, calling function resumes execution at the point of the call. It uses the return value as the result of eva luating the call operator and continues processing whatever remains of the statement in which the call was performed.
call operator 就是函数调用操作符, 一对小括号。C++(www.cppentry.com) 中的函数调用是个表达式,return 的东西就是这个表达式的值。
Parameters and Arguments
Like local variables, the parameters of a function provide named, local storage for use by the function. The difference is that parameters are defined inside the function’s parameter list and are initialized by arguments passed to the function
when the function is called.
An argument is an expression. It might be a variable, a literal constant or an expression involving one or more operators. We must pass exactly the same number of arguments as the function has parameters. The type of each argument must match the corresponding parameter in the same way that the type of an initializer must match the type of the object it initializes: The argument must have the same type or have a type that can be implicitly converted (Section 5.12, p. 178) to the parameter type. We’ll cover how arguments match a parameter in detail in Section 7.8.2 (p. 269).