# 多引數

在本篇做一個延伸的範例與 相關細節可以注意的地方。

在以下範例 修改自[上一篇](/java-08/java-08-5.md)，將示範傳遞多個引數。

```
class Circle{
	double pi=3.14;
	double radius;

	void show(){
		System.out.println("半徑＝ "+radius+"pi＝"+pi);
                System.out.println("面積＝ "+pi*radius*radius);
	}
        void set_circle(double p,double r){   //set_r 函數，用來設定pi與半徑
                pi=p;
	        radius=r;
	}
}
public class ch08_3 {

	public static void main(String[] args) {
		Circle moon=new Circle();
		moon.set_circle(3,2);
		moon.show();
	}

}
```

看到類別中 函數成員set\_circle

```
void set_circle(double p,double r){   //宣告兩個變數p與r為double型態 作為引數
                pi=p;     //再設定pi 的值為引數p
	        radius=r; //再設定radius 的值為引數r
	}
```

於主程式中，呼叫類別函數

```
moon.set_circle(3,2);   //設定moon 的pi為3，半徑為 2
```

主程式中，設定pi為3，半徑為2 會將兩數值，\
傳到類別中的函數成員 set\_circle() 的引數 p 與 r。

而在函數成員 set\_circle 中，\
程式敘述又設定 pi 的值 為引數 p = 3\
設定 radius 的值 為引數 r = 2

當主程式呼叫類別函數成員 show() 時，\
就會印出剛剛在函數成員 set\_circle 設定給 pi 與 radius 的值；\
以及利用設定後的radius算出 面積 並且印出。

## 補充：函數如果不是回傳值

必須在函數名稱前方加個 void

```
void show(){
		System.out.println("半徑＝ "+radius+"pi＝"+pi);
                System.out.println("面積＝ "+pi*radius*radius);
	}
void set_circle(double p,double r){   //set_r 函數，用來設定pi與半徑
                pi=p;
	        radius=r;
	}
```

以下介紹 有回傳值的函數：

同樣修改本篇範例

```
class Circle{
	double pi=3.14;
	double radius;

	double get_r(){
		return radius;
	}
        void set_circle(double p,double r){   //set_r 函數，用來設定pi與半徑
                pi=p;
	        radius=r;
	}
}
public class ch08_3 {

	public static void main(String[] args) {
		Circle moon=new Circle();
		moon.set_circle(3,2);
		System.out.println("半徑＝ "+moon.get_r());
	}

}
```

看到範例中函數成員 get\_r

```
double get_r(){
	return radius;
}
```

先看到程式敘述的部分：

此函數用來回傳物件的半徑，因此多一個 return可以傳回半徑

而在函數的型態部分：

因為半徑想要讓他回傳double 型態，\
因此在函數名稱之前放上 double。

最後讓我們看到主程式的部分：

```
moon.set_circle(3,2);
System.out.println("半徑＝ "+moon.get_r());
```

set\_circle 已經將pi 與radius 設值為 3 , 2\
因此在get\_r 函數中的 radius 為 2。\
在主程式呼叫函數時，就會取得該函數並且回傳剛剛存放的值

因此輸出將會是 半徑＝2.0 (因為double型態)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://java.4-x.tw/java-08/java-08-6.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
