设为首页 加入收藏

TOP

7.1.1 Function Return Type
2013-10-07 15:25:52 来源: 作者: 【 】 浏览:64
Tags:7.1.1 Function Return Type

The return type of a function can be a built-in type, such as int or double, a class type, or a compound type, such as int& or string*. A return type also can be void, which means that the function does not return a value. The following are example definitions of possible function return types:

  1. bool is_present(int *, int); // returns bool  
  2. int count(const string &, char); // returns int  
  3. Date &calendar(const char*); // returns reference to Date  
  4. void process(); // process does not return a value  


A function may not return another function or a built-in array type. Instead, the function may return a pointer to the function or to a pointer to an element in the array:

C++(www.cppentry.com) 中的函数和数组不是一等公民类型。

  1. // ok: pointer to first element of the array  
  2. int *foo_bar() { /* . . . */ } 

This function returns a pointer to int and that pointer could point to an element in an array.

We’ll learn about function pointers in Section 7.9 (p. 276).

Functions Must Specify a Return Type

It is illegal to define or declare a function without an explicit return type:

  1. // error: missing return type  
  2. test(double v1, double v2) { /* . . . */ } 

Eariler versions of C++(www.cppentry.com) would accept this programand implicitly define the return type of test as an int. Under Standard C++(www.cppentry.com), this program is an error.

In pre-Standard C++(www.cppentry.com), a function without an explicit return type was assumed to return an int. C++(www.cppentry.com) programs compiled under earlier, nonstandard compilers may still contain functions that implicitly return int.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇7. FUNCTIONS 下一篇7.1 Defining a Function

评论

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

·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)