A tour of C++ Day 2

A tour of C++ Book Note Day2

Just for share my note when I reading the book. If you want to discuss what interests you, you can write in the comments section. If there is a dispute, you can corret it!

1.3 Function

Function/declaration quote8

  • 一个函数的声明可能包含参数名,这对于编码者/阅读者是有帮助的,但是如果该函数只是一个声明/定义而非实现那么对于编译器来说,它所看见的将不会有参数名

A function declaration may contain argument names. This can be a help to the reader of a program, but unless the declaration is also a function definition, the compiler simply ignores such names. ^quote8

// if we just delclarate a function:  
double sqrt(double var);
// the compiler will ignore such names, just like:  
double sqrt(double var); -> double sqrt(double);

// but if you declarate and implement a funtion:  
double sqrt(double var) {} -> double sqrt(double var) {} // the compiler saw

Function/function_type quote9

  • 不仅仅对于值来说有类型,函数一样也是有类型的,在编译器看来,函数的类型就是函数的返回值类型 + 参数列表的各参数的类型

The type of a function consists of its return type followed by the sequence of its argument types in parentheses. ^quote9

// for exmaple, there is a function
double get(std::vector<double>& vec, int index);
// -> the type: double(std::vector<double>, int)

Function/member_function_type quote10

  • 对于类中的成员函数来说,其类型不仅仅只有返回值类型和列表类型,还需要带上类作用域来指定具体类型

For such a member function, the name of its class is also part of the function type ^quote10

// if we code a String class with a operator[]
char& String::operator[] (int index);
// -> the type: char& String::(int)

Function/trans2 quote11

  • 代码的可维护性第一步在于代码的可读性。实现可读性的第一步就是将计算任务分成各种有意义的小堆(将其以函数或者类的形式表示)并将其命名

We want our code to be comprehensible because that is the first step on the way to maintainability.
The first step to comprehensibility is to break computational tasks into meaningful chunks (represented as functions and classes) and name those. ^quote11

Function/trans3 quote12

  • 代码中的错误量与代码量和代码复杂程度有着强相关
    • 代码数量越多,就意味着代码后期维护工作量的增长,而复杂程度就不可避免的压低了代码的可读性

The number of errors in code correlates strongly with the amount of code and the complexity of the code. ^quote12

Function/trans4 quote13

  • 使用函数来做一些特殊化的任务(准确来说应该是特定的模块)通常能够让我们避免在其他代码中杂糅
    • 最简单的例子就是,模块化,每一个函数对应一个单一的功能

Using a function to do a specific task often saves us from writing a specific piece of code in the middle of other code; ^quote13

Function/trans5 quote14

  • 如果我们无法对一个函数进行合适的命名,这很有可能意味着我们对该函数模块的设计有问题
    • 显然的,如果无法对一个函数的具体作用摸棱两可,那么该函数在后续的作用也会是杂乱无章的

If we cannot find a suitable name, there is a high probability that we have a design problem. ^quote14

Function/trans6 quote15

  • 当一个函数被重载时,对于每一个基于该函数的重载函数都应该实现相同的语义
    • 也就是说,建议重载函数都是对相同功能的一个扩展,尽量不要使用重载函数去完成与原函数相悖的功能

When a function is overloaded, each function of the same name should implement the same semantics. ^quote15

// source function
void print(int) {}
// in this case, the 'print' function just want to print the value of type
void print(double) { //... }
// but if you code a 'print' function but use it to output the sum of all var of double type -> when someone use it, it will find that the function is ambiguous.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“a tour of c” 可以理解为C语言的一次巡回之旅。C语言是一种广泛应用于计算机硬件和软件开发的编程语言。它由贝尔实验室的丹尼斯·里奇于1972年开发,并在接下来的几年中逐渐流行起来。以下是关于C语言巡回之旅的一些重要内容: 首先,C语言具有简洁、高效的特点。它使用了一些基本的语法结构和关键字,使得程序代码更加简洁,易于理解和维护。同时,C语言的编译过程相对快速,生成的可执行文件也具有较高的执行效率。 其次,C语言具有广泛的应用领域。它被用于开发系统级软件,如操作系统和编译器,也被广泛应用于游戏开发、嵌入式系统、网络编程等领域。通过一次C语言的巡回之旅,可以深入了解C语言在不同领域中的应用和实践。 另外,C语言还具有丰富的库函数和标准函数供开发人员使用。这些库函数可以提供各种功能,如文件操作、字符串处理、内存管理等。通过学习和了解这些库函数,可以提高编程效率,并加快程序的开发速度。 最后,C语言也是学习其他高级编程语言的基础。许多其他编程语言,如C++、Java、Python等,都是在C语言基础上发展起来的。因此,通过一次C语言的巡回之旅,可以掌握一些基本的编程概念和思维方式,为学习其他编程语言打下坚实的基础。 总之,C语言巡回之旅是一个了解C语言的机会,通过深入学习和实践,可以了解C语言的特点、应用及其在计算机科学领域的重要性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值