设为首页 加入收藏

TOP

7.1.2 Function Parameter List
2013-10-07 15:25:47 来源: 作者: 【 】 浏览:77
Tags:7.1.2 Function Parameter List

The parameter list of a function can be empty but cannot be omitted. A function with no parameters can be written either with an empty parameter list or a parameter list containing the single keyword void. For example, the following declarations of process are equivalent:

  1. void process() { /* . . . */ } // implicit void parameter list  
  2. void process(void){ /* . . . */ } // equivalent declaration 

A parameter list consists of a comma-separated list of parameter types and (optional) parameter names. Even when the types of two parameters are the same, the type must be repeated:

  1. int manip(int v1, v2) { /* . . . */ } // error  
  2. int manip(int v1, int v2) { /* . . . */ } // ok 

No two parameters can have the same name. Similarly, a variable local to a function may not use the same name as the name of any of the function’s parameters. Names are optional, but in a function definition, normally all parameters are named. A parameter must be named to be used.

Parameter Type-Checking

C++(www.cppentry.com) is a statically typed language (Section 2.3, p. 44). The arguments of every call are checked during compilation.

C++(www.cppentry.com) 程序员的一项基本功是检查函数参数类型,并且在出现编译错误的时候修正形参或实参,使程序正常编译并运行。

Whenwe call a function, the type of each argumentmust be either the same type as the corresponding parameter or a type that can be converted (Section 5.12, p. 178) to that type. The function’s parameter list provides the compiler with the type information needed to check the arguments. For example, the function gcd, which we defined on page 226, takes two parameters of type int:

  1. gcd("hello""world"); // error: wrong argument types  
  2. gcd(24312); // error: too few arguments  
  3. gcd(42, 10, 0); // error: too many arguments 

Each of these calls is a compile-time error. In the first call, the arguments are of type const char*. There is no conversion from const char* to int, so the call is illegal. In the second and third calls, gcd is passed the wrong number of arguments. The function must be called with two arguments; it is an error to call it with any other number.

But what happens if the call supplies two arguments of type double Is this call legal

  1. gcd(3.14, 6.29); // ok: arguments are converted to int 

In C++(www.cppentry.com), the answer is yes; the call is legal. In Section 5.12.1 (p. 179) we saw that a value of type double can be converted to a value of type int. This call involves such a conversion—we want to use double values to initialize int objects. Therefore, flagging the call as an error would be too severe. Rather, the arguments are implicitly converted to int (through truncation). Because this conversion might lose precision, most compilers will issue a warning. In this case, the call becomes

  1. gcd(3, 6); 

and returns a value of 3.

A call that passes too many arguments, omits an argument, or passes an argument of the wrong type almost certainly would result in serious run-time errors. Catching these so-called interface errors at compile time greatly reduces the compile-debug-test cycle for large programs.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇7.1 Defining a Function 下一篇1.1.1 Java与C和C++的关系

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)