CONTENTS
Section 7.1 Defining a Function . . . . . . . . . . . . . 226
Section 7.2 Argument Passing . . . . . . . . . . . . . . 229
Section 7.3 The returnStatement . . . . . . . . . . . . 245
Section 7.4 Function Declarations . . . . . . . . . . . . 251
Section7.5 LocalObjects . . . . . . . . . . . . . . . . . 254
Section7.6 InlineFunctions . . . . . . . . . . . . . . . . 256
Section 7.7 ClassMember Functions . . . . . . . . . . 258
Section 7.8 Overloaded Functions . . . . . . . . . . . . 265
Section 7.9 Pointers to Functions . . . . . . . . . . . . . 276
ChapterSummary . . . . . . . . . . . . . . . . . . . . . . 280
DefinedTerms . . . . . . . . . . . . . . . . . . . . . . . . . 280
This chapter describes how to define and declare functions. We’ll cover how arguments are passed to and values are returned from a function. We’ll then look at three special kinds of functions: inline functions, class member functions, and overloaded functions. The chapter closes with a more advanced topic: function pointers.
函数是程序组织的基本单元。从本章开始,C++(www.cppentry.com)的一些特性开始体现出来。本章用了约20 页的篇幅来讲解函数调用时的实参传递(俗称参数传递、传参)和return 语句。这部分内容非常重要,特别是当参数类型或函数返回类型是class类型时,C++(www.cppentry.com) 的行为非常独特,值得认真学习。
A function can be thought of as a programmer-defined operation. Like the built-in operators, each function performs some computation and (usually) yields a result. Unlike the operators, functions have names and may take an unlimited number of operands. Like operators, functions can be overloaded, meaning that the same name may refer to multiple different functions.
本书严格区分形参(parameter)和实参(argument)这两个术语,前者就像函数的局部变量,后者是调用函数时传入的数据;或者说,形参是变量声明,而实参是表达式。