# 參數 (程式設計)

## 示例

double SalesTax( double price ){
return 0.05 * price;
}

SalesTax(10.00);

## 參數和引數

{
//在運行時，我們使用的值是一個引數。
//如上所述，在討論子例程定義時保留術語參數。
}

int Sum( int addend1, int addend2 )
{
}

int value1 = 40;
int value2 = 2;
int sum_value = Sum(value1, value2);

## 引數傳遞

### 預設參數

PowerShell示例：

function doc(\$g = 1.21) {
"\$g gigawatts? \$g gigawatts? Great Scott!"
}
PS  > doc
1.21 gigawatts? 1.21 gigawatts? Great Scott!

PS  > doc 88
88 gigawatts? 88 gigawatts? Great Scott!

### 可變長度參數列表

PowerShell示例：

function marty {
\$args | foreach { "back to the year \$_" }
}
PS  > marty 1985
back to the year 1985

PS  > marty 2015 1985 1955
back to the year 2015
back to the year 1985
back to the year 1955

### 命名參數

PowerShell示例：

"Young Jennifer: I'm \$adjectiveYoung!"
"Old Jennifer: I'm \$adjectiveOld!"
}
PS  > jennifer 'fresh' 'experienced'
Young Jennifer: I'm fresh!
Old Jennifer: I'm experienced!

PS  > jennifer -adjectiveOld 'experienced' -adjectiveYoung 'fresh'
Young Jennifer: I'm fresh!
Old Jennifer: I'm experienced!

## 輸出參數

TextExtent(WString text, Font font : Integer width, Integer height)

function f(x, y: integer): integer;
begin
f := x + y;
end;

### 使用

int width
int height;

F(x, &width, &height);

public static bool TryParse(string s, out int result)

int result;
if (!Int32.TryParse(s, result)) {
// exception handling
}

### 缺點

Object obj = G(y, F(x));

Object obj;
F(x, &obj);
G(y, &obj);

Object obj;
G(y, F(x, &obj));

### 替代方式

def f():
return 1, 2
a, b = f()

result = Parse(s)
if result is None:
# exception handling

try:
result = Parse(s)
except ParseError:
# exception handling

WidthHeight width_and_height = F(x);

## 參考資料

1. ^ Prata, Stephen. C primer plus 5th. Sams. 2004: 276–277. ISBN 978-0-672-32696-7.
2. ^ Working Draft, Standard for Programming Language C++ (PDF). www.open-std.org. [1 January 2018]. （原始内容 (PDF)存档于2005-12-14）.
3. ^ Gordon, Aaron. Subprograms and Parameter Passing. rowdysites.msudenver.edu/~gordona. [1 January 2018]. （原始内容存档于2018-01-01）.
4. ^ KathleenDollard. Passing Arguments by Value and by Reference - Visual Basic. docs.microsoft.com. [2020-04-21]. （原始内容存档于2019-09-06） （美国英语）.
5. ^ The GNU C Programming Tutorial. crasseux.com. [2020-04-21]. （原始内容存档于2020-02-16）.
6. Ada '83 Rationale, Sec 8.2: Parameter Modes. archive.adaic.com. [2020-04-21]. （原始内容存档于2013-10-06）.
7. PL/SQL Subprograms. docs.oracle.com. [2020-04-21]. （原始内容存档于2020-06-14）.
8. Msdn forums - Visual C# Language. social.msdn.microsoft.com. [2020-04-21]. （原始内容存档于2019-09-04）.
9. ^ stevestein. ParameterDirection Enum (System.Data). docs.microsoft.com. [2020-04-21] （美国英语）.
10. ^ Functions — The Swift Programming Language (Swift 5.2). docs.swift.org. [2020-04-21]. （原始内容存档于2020-03-29）.
11. ^ dotnet-bot. Int32.TryParse Method (System). docs.microsoft.com. [2020-04-21] （美国英语）.
12. jillre. CA1021: Avoid out parameters - Visual Studio 2015. docs.microsoft.com. [2020-04-21] （美国英语）.