Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

4.5.2 函数声明

你是否注意到,函数调用所需的所有信息都已经包括在函数定义的第一行?例如,

根据这些信息,可以写出如下语句:

我们不需要知道函数体是如何实现的。在编写程序的时候,我们一般不需要知道函数体的实现细节。为什么我们要知道标准库函数sqrt()是如何实现的呢?我们知道它能够计算参数的平方根就够了。为什么我们要阅读square()函数的代码呢?虽然我们可能会好奇它的具体实现,但大多数情况下,我们仅仅关心如何调用函数就可以了。幸运的是,C++提供了一种与函数定义分离的方法来显示函数的信息,称为函数声明(function declaration)。

注意,函数原型以分号结束,分号替代了函数定义中的函数体部分:

如果你想使用某个函数,可以在代码中声明或者通过#include包含该函数的函数原型,而函数的定义可以在程序的其他部分,我们将在8.3节和8.7节中讨论具体的实现细节。函数原型和函数定义的分离对于大型程序是非常必要的,我们可以用函数原型来保证代码的简洁,从而保证在同一时刻将注意力集中在程序的某一局部区域上(4.2节)。 ...

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

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

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

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252