---
tags: C++, 程式設計
---
# C++ 程式設計 函數 (上)
## 一、函式說明
### (一)說明:
#### 1、函式式什麼
像是請打手幫忙,例如要算sin30度,請一位三角函數大師,只要告訴他sin30度,他就會直接告訴你答案。
#### 2、使用方法
##### (1) void (不用回傳值)
void 名稱 (參數){
執行內容;
}
參數預設為空,屆時看程式執行呼叫函式的值作為參數。
若有預設參數,屆時看執行程式呼叫函式是否有給值,若有給使用給定的值,若沒有給值則使用預設值。
##### (2) int,double,float...(須回傳值)
```cpp=
int fucn(){
return 0 ;
}
float fun2(){
return 3.8;
}
```
### (二)函式位置
#### 1、放置正確位置
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
void sayhello(){ //宣告打手名字
cout << "hello!" << endl; //打手會做什麼
}
//------main ( )主程式的部分------
int main (){
sayhello(); //呼叫sayhello打手幫忙
}
//output: hello!
```
#### 2、若位置有誤
##### (1)發生error因為函式不在指定位置
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
//------main ( )主程式的部分------
int main (){
sayhello(); //呼叫sayhello打手幫忙
}
void sayhello(){ //宣告打手名字
cout << "hello!" << endl; //打手會做什麼
}
```
##### (2)改善
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
void sayhello() //宣告函式,告訴程式我有一個函式sayhello(),請他去程式裡面找。
//------main ( )主程式的部分------
int main (){
sayhello(); //呼叫sayhello打手幫忙
}
void sayhello(){ //宣告打手名字
cout << "hello!" << endl; //打手會做什麼
}
//output: hello!
```
### (三)函數參數
#### 1、參數說明
呼叫函式執行時會(參考的數值)簡稱參數,間而影響函式執行內容。
參數預設為空,屆時看程式執行呼叫函式的值作為參數。
若有預設參數,屆時看執行程式呼叫函式是否有給值,若有給使用給定的值,若沒有給值則使用預設值。
#### 2、範例
##### 【未給預設值】
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
void showDouble(int i){
cout << "double" << i << "is" << 2*i << endl;
}
//------main ( )主程式的部分------
int main (){
showdouble(33); //呼叫showdouble打手幫忙
}
//output: double 33 is 66 ;
```
##### 【有預設值】
```cpp=
【未給預設值】
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
void showDouble(int i =100){
cout << "double" << i << "is" << 2*i << endl;
}
//------main ( )主程式的部分------
int main (){
showdouble(33); //呼叫showdouble打手幫忙
showdouble(); /未給參數值則使用預設值運算/
}
//output: double 33 is 66 ;
//output: double 100 is 200 ;
```
##### 也可以一次設計多個參數
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
double new(double a,double b, double c){
return a+b*c;
}
//------main ( )主程式的部分------
int main (){
double d = new(1,2,3);
cout << "計算結果" << d << endl;
//方法二值皆呼叫函數
//cout << "計算結果" << new(1,2,3) << end;
}
```
### (四)函數內的變數沒有儲存空間
#### 假設函式如下
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
void adding(){
int i = 100;
cout << " i= " << i << endl;
i++;
}
//------main ( )主程式的部分------
int main (){
adding();
adding();
}
// output: i=100
i=100
//因為函數內的變數沒有儲存空間
```
#### 改善一:設定靜態變數
```cpp=
#include <iostream>
using namespace std;
//------函式的部分(main之上)------
void adding(){
static int i = 100;
cout << " i= " << i << endl;
i++;
}
//------main ( )主程式的部分------
int main (){
adding();
adding();
}
// output: i=100
i=101
//函數內設定靜態變數(會準備記憶空間給變數)
```
#### 改善二:全域變數
```cpp=
#include <iostream>
using namespace std;
int i = 100;
//------函式的部分(main之上)------
void adding(){
int i = 100;
cout << " i= " << i << endl;
i++;
}
//------main ( )主程式的部分------
int main (){
adding();
adding();
}
// output: i=100
i=101
//函數內設定靜態變數(會準備記憶空間給變數)
```
## 二、練習
### (一)題目一:
試寫出一個函式,可印出指定行數的「Hello C++!」訊息
#### 1、程式碼(答案不唯一):
```cpp=
#include<iostream>
using namespace std;
/*題目1:試寫出一個函式,可印出指定行數的「Hello C++!」訊息。*/
voidHello(inta) {
while (a> 0) {
cout<<"Hellow C++!"<<endl;
a -= 1;
}
}
int main() {
inta,b;
cout<<"請輸入想列印的行數:";
cin>> a;
Hello(a);
}
```
#### 2、圖示:

### (二)題目二:
請寫出一個比較大小的函式「getMax」,由鍵盤分別輸入兩個數後,將兩個數當成「輸入參數」呼叫該函式。
函式最後會回傳較大值
#### 1、程式碼(答案不唯一):
```cpp=
#include<iostream>
using namespace std;
/*題目2:請寫出一個比較大小的函式「getMax」,由鍵盤分別輸入兩個數後,將兩個數當成「輸入參數」呼叫該函式。
函式最後會回傳較大值。*/
void getMax(int a,int b) {
if (a>b)
cout<<"較大值為:"<<a;
elseif (a<b)
cout<<"較大值為:"<<b;
else
cout<<"較大值為:"<<"兩數一樣大";
}
int main() {
int a, b;
cout<<"請輸入想比較大小的數字1:";
cin>> a;
cout<<"請輸入想比較大小的數字2:";
cin>> b;
getMax(a,b);
}
```
#### 2、圖示:
