## 基本概念 一個可重複使用的程式區塊,透過參數與回傳值來傳遞資料 ## lambda 就是一個函式只是他的寫法比函式更簡潔,而且更加的好用和維護。 ### 格式 ```cpp= auto l = [ captures ] ( parameters ) -> return_type { body } ``` :::success 1. auto 可以用 `std::function` 中的 `std::function<int(int, int)>` 代替 2. `[]` 裡面可以放 `&`、`=` 或是不放也可以(預設取副本變數) - `&` : 引用變數 => **改變**變數數值 (全部) - `=` : 取變數副本 => 複製原本變數數值 (全部) - 變數值: 如果要放的話要**事先定義**喔 3. `-> return_type` **可以省略** (通常不會寫) ::: ## operate 用來預處理不是預設資料型態比較的方式 ## 用途 1. 遞迴 2. 減少主程式程式碼行數 (如果某個函式要執行多次) 3. 有多個回傳值 4. 可以加速程式碼執行效率 5. 讓主程式碼更簡潔 <img src="https://raw.githubusercontent.com/EricbobXD/C_plus_plus_Blockly/main/photo/function.png"></img>
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up