Functions¶
Include¶
- Built-in C++ system library
- local library
Using¶
- Many libraries separate their symbols (variables, functions, etc.) into namespaces to avoid conflicts
using namespace
brings symbols from a library's namespace into the global scope (全局作用域)- Common usage:
using namespace std;
- Common usage:
- 若不使用
using
声明,在调用函数时需要加上<namespace name>::
前缀
Console I/O¶
cout
¶
cin
¶
不太推荐,因为没有输入格式检查。
课程中使用 Stanford Library 里的函数。
Forward declarations¶
在函数定义之前声明函数,也称为函数原型(function prototype)
Default parameters¶
e.g.
// Print a line of characters of the given length
void printLine(int length = 5, char ch = '*'){
for (int i = 0; i < length; i++){
cout << ch;
}
}
int main(){
printLine(); // *****
printLine(3); // ***
printLine(4, '#'); // ####
}