1. 程式人生 > 其它 >《C++ Primer Plus》第六版筆記

《C++ Primer Plus》第六版筆記

之前學過一段,現在總結,並且往後學。

第七章 函式——C++的程式設計模組

7.1函式的基本知識,書中複習

  • 庫函式是已經定義和編譯好的引數,使用標準標頭檔案提供原型。

#include<iostream>

  • 原型中引數列表是佔位符,可以與函式定義中的變數名不同。

void fun(int a);

void fun(int abc)

{程式碼塊;}

  • 因函式過載的二義性,所以函式引數不允許強制轉換

// 使用原型和函式呼叫
#include <iostream>
void cheers(int); // 原型:沒有返回值
double cube(double x); // Prototype:返回一個double值
int main()
{
using namespace std;

cheers(cube(125));

return 0;
}

void cheers(int n)
{
using namespace std;
cout << "n = " << n << endl;
}

double cube(double x)
{
return x * x * x;
}

執行結果

n = 1953125

7.2函式引數和按值傳遞