函式&遞迴

秋遊

萬眾期待的秋遊資訊來囉((o(*>ω<*)o))
二段後的好去處!!ᕕ( ՞ ᗜ ՞ )ᕗ
快來跟我們一起烤肉&打雷射槍戰吧~Ψ(≧ω≦)Ψ
報名只到11/17(日)23:59(*ゝωб*)b
快揪你社內的朋友一起來報名吧!
٩( ᐛ )( ᐖ )۶

社課已經默默過了一半了\\\\٩( 'ω' )و ////
想知道大家對社團的想法💡
大家有空可以填這個回饋表單哦(匿名的)

回饋表單

函式

什麼是函式?

飲料製作

公式

返回型態 函式名稱(參數列表) {

     // 函式的程式碼

     return 返回值;

}

int add(int a, int b) {
    return a + b;
}

宣告+定義

#include <iostream>
using namespace std;

int max(int x, int y){
    if(x>y){
    	return x;
    }
    else{
    	return y;
    }
}

int main()
{
    int a1, b1;
    cin >> a1 >> b1;
    cout << max(a1, b1) << endl;
    return 0;
}
#include <iostream>
using namespace std;

int max(int x, int y);

int main()
{
    int a1, b1, a2, b2;
    cin >> a >> b;
    cout << max(a1, b1) << endl;
    return 0;
}

int max(int x, int y){
    if(x>y){
    	return x;
    }
    else{
    	return y;
    }
}

不回傳值 :

void

#include <iostream>
using namespace std;

void max(int x, int y){
    if(x>y){
    	cout << x << endl;;
    }
    else{
    	cout << y << endl;
    }
}

int main()
{
    int a1, b1;
    cin >> a1 >> b1;
    max(a1, b1);
    return 0;
}

&a(參考傳遞)

#include <iostream>
using namespace std;

void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 5;
    int y = 10;
    cout << "Before swap: x = " << x << ", y = " << y << endl;
    swap(x, y);
    cout << "After swap: x = " << x << ", y = " << y << endl;
    return 0;
}

輸出:
Before swap: x = 5, y = 10
After swap: x = 10, y = 5

&a(參考傳遞)

#include <iostream>
using namespace std;

void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int main(){
	int x=2, y=5;
	swap(x, y);
    cout << x << ' ' << y << endl;
}

輸出 : 2 5

int a, int b  : 創建 a 和 b 的副本

int &a, int &b  :  傳遞變數的實際記憶體位置

優點

  • 避免編寫重複的程式碼

  • 方便閱讀程式

  • 方便除錯(Debug)

  • 有助於分工協作

  • 節省記憶體和執行效率

函式        工具

函式庫        工具箱

C++函式庫

 #include <iostream>

                  <string>

                  <ctime>

                  <algorithm>

                  <cmath>

                  <vector>

 <cmath>

 abs(x) x的絕對值

 sqrt(x) x的平方根

 pow(a,x) a的x次方

#include <bits/stdc++.h>

用法 : 變數.函式名稱()

#include <iostream> // 用於輸入輸出
#include <cmath>    // 用於數學運算

using namespace std;

int main() {
    double number;
    cin >> number;

    // 使用 <cmath> 函式庫中的 sqrt 函數計算平方根
    double squareRoot = sqrt(number);

    // 輸出結果
    cout << "數字 " << number << " 的平方根是 " << squareRoot << endl;

    return 0;
}

小試牛刀 1

題目:飲料價格計算器

某家飲料店需要開發一個小型的計算系統,方便計算顧客訂購的飲料價格。這家店有以下幾種飲料:

a 30元 / b 35元 / c 45元 / d 50元

請設計一個程式,接收一個表示飲料名稱的字串參數,並返回該飲料的價格。若沒有這種飲料則輸出「此飲料不在選單上」。

#include <iostream>
using namespace std;

// 定義 calculatePrice 函式
int menu(int drink) {
    if (drink == 'a') return 30;
    else if (drink == 'b') return 35;
    else if (drink == 'c') return 45;
    else if (drink == 'd') return 50;
    else return 0; // 若飲料名稱不在選單中,返回 0
}

int main() {
    char mydrink;
    cin >> mydrink;

    int price = menu(mydrink);
    if (price > 0) cout << mydrink << "的價格為:" << price << " 元" << endl;
    else cout << "此飲料不在選單上" << endl;
    return 0;
}

遞迴

什麼是遞迴?

  • 把大問題拆成小問題

  • 在函式裡呼叫自己

int factorial(int n){
    if(n==1) return 1; //1!為1
    else return n*factorial(n-1); //n!為n*(n-1)!
}

n

n*(n-1)

n*(n-1)*(n-2)

n*(n-1)*(n-2)*(n-3)

......

n*(n-1)*(n-2)*(n-3)*(n-4)

n*(n-1)*(n-2)*(n-3)*(n-4)*...1

小試牛刀2

費式數列

 F(0)=0 、 F(1)=1、... F(n) = F(n-1) + F(n-2)

(之後每一項都是前兩項相加 : 0 1 1 2 3 5 8 13...)

題目 : 給一個整數m,請輸出m項的費式數列

#include <iostream>
using namespace std;

// 定義遞迴函式計算費式數列
int sequence(int n) {
    if (n == 1) return 0; 
    else if (n == 2) return 1; 
    else return sequence(n - 1) + sequence(n - 2); // 遞迴計算
}

int main() {
    int n;
    cin >> n;

    cout << sequence(n) << endl;
    return 0;
}

假設n=7

f(7)

f(6)

f(5)

f(5)

f(4)

f(4)

f(3)

f(3)

f(3)

f(3)

f(2)

f(4)

f(2)

f(2)

f(1)

0

1

1

1

1

1

1

2

3

2

2

1

5

3

8

2

1

1

1

1

1

1

0

kahoot!!!!!

deck

By phoebe tsai

deck

  • 309