S SmartDocs
シリーズ: C++ cpp 141 行 · 更新日 2026-04-03

functions_basic.cpp

C++/Part1_基礎入門/Ch04_函式/functions_basic.cpp

// ============================================================
// Ch04 — 函式基礎(Functions Basics)
// 編譯:g++ -std=c++17 -Wall -o functions_basic functions_basic.cpp
// ============================================================

#include <iostream>
#include <string>

// ── 函式原型(宣告) ──────────────────────────────
double rectangleArea(double width, double height);
double circleArea(double radius);
void swapByValue(int a, int b);
void swapByRef(int& a, int& b);
void swapByPtr(int* a, int* b);
double celsiusToFahrenheit(double celsius);
double fahrenheitToCelsius(double fahrenheit);
void printLine(char ch = '-', int count = 40);
void greet(const std::string& name = "同學");

// ── 主程式 ────────────────────────────────────────
int main() {
    std::cout << "===== Ch04:函式基礎 =====\n\n";

    // ── 1. 基本函式呼叫與回傳值 ──
    std::cout << "【1】基本函式呼叫\n";
    double w = 5.0, h = 3.0;
    std::cout << "長方形面積(" << w << " x " << h << ")= "
              << rectangleArea(w, h) << "\n";

    double r = 4.0;
    std::cout << "圓形面積(半徑 " << r << ")= "
              << circleArea(r) << "\n\n";

    // ── 2. void 函式 ──
    std::cout << "【2】void 函式(無回傳值)\n";
    greet("小明");
    greet();  // 使用預設參數
    std::cout << "\n";

    // ── 3. 傳值 vs 傳參考 vs 傳指標 ──
    std::cout << "【3】參數傳遞方式比較\n";

    int x = 10, y = 20;
    std::cout << "交換前:x = " << x << ", y = " << y << "\n";

    // 3a. 傳值 — 不會改變原始值
    swapByValue(x, y);
    std::cout << "傳值交換後:x = " << x << ", y = " << y
              << "(原始值不變)\n";

    // 3b. 傳參考 — 會改變原始值
    swapByRef(x, y);
    std::cout << "傳參考交換後:x = " << x << ", y = " << y
              << "(原始值已交換)\n";

    // 3c. 傳指標 — 也會改變原始值
    swapByPtr(&x, &y);
    std::cout << "傳指標交換後:x = " << x << ", y = " << y
              << "(再次交換回來)\n\n";

    // ── 4. 溫度轉換 ──
    std::cout << "【4】溫度轉換\n";
    double tempC = 100.0;
    double tempF = celsiusToFahrenheit(tempC);
    std::cout << tempC << "°C = " << tempF << "°F\n";
    std::cout << tempF << "°F = " << fahrenheitToCelsius(tempF) << "°C\n\n";

    // ── 5. 預設參數 ──
    std::cout << "【5】預設參數\n";
    printLine();           // 全部使用預設值:'-' 重複 40 次
    printLine('=');        // count 使用預設值
    printLine('*', 20);   // 不使用任何預設值
    std::cout << "\n";

    // ── 6. const 參考參數 ──
    std::cout << "【6】const 參考參數\n";
    std::string msg = "Hello, C++!";
    // greet 接受 const std::string& 避免複製
    std::cout << "字串長度:" << msg.length() << "\n";
    greet(msg);

    return 0;
}

// ── 函式定義 ──────────────────────────────────────

// 計算長方形面積
double rectangleArea(double width, double height) {
    return width * height;
}

// 計算圓形面積
double circleArea(double radius) {
    const double PI = 3.14159265358979;
    return PI * radius * radius;
}

// 傳值交換 — 不影響外部變數
void swapByValue(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
    // a, b 只是副本,函式結束後就消失了
}

// 傳參考交換 — 直接操作原始變數
void swapByRef(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

// 傳指標交換 — 透過指標操作原始變數
void swapByPtr(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// 攝氏轉華氏
double celsiusToFahrenheit(double celsius) {
    return celsius * 9.0 / 5.0 + 32.0;
}

// 華氏轉攝氏
double fahrenheitToCelsius(double fahrenheit) {
    return (fahrenheit - 32.0) * 5.0 / 9.0;
}

// 列印一行字元(有預設參數)
void printLine(char ch, int count) {
    for (int i = 0; i < count; i++) {
        std::cout << ch;
    }
    std::cout << '\n';
}

// 打招呼(const 參考 + 預設參數)
void greet(const std::string& name) {
    std::cout << "你好," << name << "!歡迎學習 C++。\n";
}

関連記事