Skip to Content
C++语言导学(原书第2版)
book

C++语言导学(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
5h 35m
Chinese
Pearson
Content preview from C++语言导学(原书第2版)

1.3 函数

在C++程序中完成某些任务的主要方式就是调用函数。你若想描述如何进行某个操作,把它定义成函数是标准方式。注意,函数必须先声明后调用。

一个函数声明需要给出三部分信息:函数的名字、函数的返回值类型(如果有的话)以及调用该函数必须提供的参数数量和类型。例如:

在一个函数声明中,返回类型位于函数名之前,参数类型位于函数名之后,并用括号包围起来。

参数传递的语义与初始化的语义是相同的(参见3.6.1节)。即,编译器会检查参数的类型,并且在必要时执行隐式参数类型转换(参见1.4节)。例如:

我们不应低估这种编译时检查和类型转换的价值。

函数声明可以包含参数名,这有助于读者理解程序的含义。但实际上,除非该声明同时也是函数的定义,否则编译器会简单忽略参数名。例如:

返回类型和参数类型属于函数类型的一部分。例如:

函数可以是类的成员(参见2.3节和4.2.1节)。对这种成员函数(member function),类名也是函数类型的一部分,例如:

我们都希望自己的代码易于理解,因为这是提高代码可维护性的第一步。而令程序易于理解的第一步,就是将计算任务分解为有意义的模块(用函数和类表达)并为它们命名。这样的函数就提供了计算的基本词汇,就像类型(包括内置类型和用户自定义类型)提供了数据的基本词汇一样。C++标准算法(如find、sort和iota)提供了一个良好开端(参见第12章),接下来我们就能用这些表示通用或者特殊任务的函数组合出更复杂的计算模块了。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111633280