# C++ 隨手筆記#2 >前一篇 [C++ 隨手筆記#1](https://hackmd.io/@Neroal/H1PeMSyBI) [TOC] ## Function使用 ### 甚麼是Function >[color=Red]跑步、倒垃圾、滑手機這些都是一個 ==**動作**==,而**Function**就是把數行的程式碼編列成一個動作,讓我們在程式裡可以重複地去 ==**呼叫**== 它 >[color=Orange]如果沒有Function,我們的程式會看起來雜亂無章如下方程式: ``` C++= int main() { int a=1; int b=2; a = a + b; a = a + b; a = a + b; a = a - b; a = a + b; . . . } ``` ### 使用Function >[color=Yellow]宣告Function的方式為 ==**資料型態**== ==**命名**== ,目前C++有兩種宣告方法: 1. 在**main**之前 ``` C++= include <iostream> using namespace std; void Hello() { cout << "Hello" << endl; } void World() { cout << "World" << endl; } int main() { Hello(); World(); } ``` 2. 在**main**之後,但是要事先宣告 ``` C++= include <iostream> using namespace std; void Hello(); void World(); int main() { Hello(); World(); } void Hello() { cout << "Hello" << endl; } void World() { cout << "World" << endl; } ``` :::warning 如果是第二種方法,但是沒有提前宣告在前方,編譯器可是會生氣的喔:exclamation: ::: ## while迴圈 ### 甚麼是while迴圈 >[color=red]當你想要==重複執行==某個程式,但是又想要自動讓它結束時。**while**會是一個不錯的選擇 >[color=orange] 你可以設立一個條件,當==條件符合==時,會在這個迴圈裡面一直重複執行,直到不符合條件為止 ### 使用方式 >[color=yellow]目前分為兩種while迴圈: 1. **while(條件)** ```C++ int i=0; while(i<5) { i++; } ``` 2. **do while(條件)** ```C++ int i=0; do { i++; }while(i<5); ``` :::info 兩者之間差異在於第一種會**先判斷條件後執行**,第二種會**先執行後判斷條件** :mega: ::: :::info **continue**和**break**的差異:使用**continue**時,會忽略下方指令直接進行下一次的迴圈;使用**break**時,會強制跳出迴圈:exclamation: ::: ## For loop迴圈 ### 甚麼是For loop迴圈 >[color=lightgreen]看到這邊,應該已經有不少人問跟剛剛的**while迴圈**差在哪裡?我曾經也被這個問題困擾了許久,得到的結論是,==固定次數用**for loop**,未知次數用**while loop**==(雙押 :smile: ) ### 使用方式 >[color=lightblue]通常用法會像是 ==**for(初始值;結束條件;初始值變化)**== >[color=purple]下方範例當迴圈完成所有指令時,會重複執行並且變數**i**會+1,結束條件是加到**i** >=10就會結束 ```C++ for(int i=0;i<10;i++) { //do something... } ``` ## 參考Reference ### 甚麼是reference >[color=red]簡單來說就是變數的**別名(alias)**,但這樣講一定還是會有人聽不懂。舉個例子,王阿呆今天**改名**成王小白,媽媽發了1000塊給王小白,請問王阿呆拿到了多少錢? ### 使用方式 >[color=orange]用上面的例子作為範例,通常用法會像是 ==資料型態==**&** ==王小白== **=** ==王阿呆== ```c++ #include <iostream> using namespace std; int main() { int Myint = 0; int& Refint = Myint; Refint += 1000; cout << Myint << endl; cout << Refint << endl; system("pause"); } ``` 輸出結果 :tv:  ### Call by reference >[color=yellow]接著我們把剛剛所學做進一步的延伸,**reference**也可以應用在**Function**上。但在這之前,要先讓大家了解 **call by reference** 和 **call by value** 的差異 - **Call by Value** ```c++ #include <iostream> using namespace std; void Function(int a) { a++; } int main() { int a=0; Function(a); cout << a << endl; system("pause"); } ``` 輸出結果 :tv:  - **Call by reference** ```c++ #include <iostream> using namespace std; void Function(int& a) { a++; } int main() { int a=0; Function(a); cout << a << endl; system("pause"); } ``` 輸出結果 :tv:  :::info **TIP** :mega: Call by value輸入為一個數值,雖然有在Function裡面加1,但是卻不會回傳給**a** Call by reference輸入為一個reference,就相當於改名一樣,所以在Function 裡的改變,原本的**a**也會跟著改變 ::: ## 多載Overlording ### 甚麼是 Overlording >[color=red]同樣名稱的函數擁有==多種格式==,就像是手機一樣,雖然都叫手機卻有不同的款式 ### 使用方式 >[color=orange]就跟一般函數的命名方式一樣,但是在輸入格式的部分要不同 ```c++ #include <iostream> #include <string> using namespace std; void Print(int a) { cout << a << endl; } void Print(string a) { cout << a << endl; } void Print(string a,int b) { cout << a << "+" << b << endl; } int main() { int number = 8; string name = "Neroal"; Print(number); Print(name); Print(name,number); system("pause"); } ``` 輸出結果 :tv:  :::warning 目前C++**多載Overlording**只能用在 **無回傳(void)** 的函數上,如果套用在 **回傳(DataType)** 的函數上,編譯器會生氣喔:exclamation: ::: >下一篇 [C++ 隨手筆記#3](https://hackmd.io/@Neroal/BkJsPoxSI) ###### tags: `C++` `function` `while` `for` `Reference` `Overlording`
×
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