# 函式運算式 (Visual Basic)Function Expression (Visual Basic)

## 語法Syntax

Function ( [ parameterlist ] ) expression
- or -
Function ( [ parameterlist ] )
[ statements ]
End Function


## 組件Parts

parameterlist 選擇性。Optional. 本機變數名稱的清單，代表此程式的參數。A list of local variable names that represent the parameters of this procedure. 即使清單是空的，括弧也必須存在。The parentheses must be present even when the list is empty. 請參閱參數清單See Parameter List.
expression 必要。Required. 單一運算式。A single expression. 運算式的類型是函式的傳回型別。The type of the expression is the return type of the function.
statements 必要。Required. 使用 Return 語句來傳回值的語句清單。A list of statements that returns a value by using the Return statement. （請參閱Return 語句）。傳回值的類型是函式的傳回型別。(See Return Statement.) The type of the value returned is the return type of the function.

## 備註Remarks

Lambda 運算式是沒有名稱的函式，會計算並傳回值。A lambda expression is a function without a name that calculates and returns a value. 除了 RemoveHandler的引數以外，您可以在任何可使用委派類型的位置使用 lambda 運算式。You can use a lambda expression anywhere you can use a delegate type, except as an argument to RemoveHandler. 如需委派的詳細資訊，以及搭配使用 lambda 運算式和委派的用法，請參閱委派語句寬鬆委派轉換For more information about delegates, and the use of lambda expressions with delegates, see Delegate Statement and Relaxed Delegate Conversion.

## Lambda 運算式語法Lambda Expression Syntax

Lambda 運算式的語法類似于標準函式。The syntax of a lambda expression resembles that of a standard function. 差異如下：The differences are as follows:

• Lambda 運算式沒有名稱。A lambda expression does not have a name.

• Lambda 運算式不能有修飾詞，例如 OverloadsOverridesLambda expressions cannot have modifiers, such as Overloads or Overrides.

• Lambda 運算式不會使用 As 子句來指定函式的傳回型別。Lambda expressions do not use an As clause to designate the return type of the function. 相反地，型別是從單行 lambda 運算式的主體評估為的值，或多行 lambda 運算式的傳回值推斷而來。Instead, the type is inferred from the value that the body of a single-line lambda expression evaluates to, or the return value of a multiline lambda expression. 例如，如果單行 lambda 運算式的主體是 Where cust.City = "London"，則其傳回型別會 BooleanFor example, if the body of a single-line lambda expression is Where cust.City = "London", its return type is Boolean.

• 單行 lambda 運算式的主體必須是運算式，而不是語句。The body of a single-line lambda expression must be an expression, not a statement. 主體可以包含函式程式的呼叫，而不是對 sub 程式的呼叫。The body can consist of a call to a function procedure, but not a call to a sub procedure.

• 所有參數都必須具有指定的資料類型，否則必須推斷全部。Either all parameters must have specified data types or all must be inferred.

• 不允許選擇性和 Paramarray 參數。Optional and Paramarray parameters are not permitted.

• 不允許泛型參數。Generic parameters are not permitted.

## 範例Example

Dim add1 = Function(num As Integer) num + 1

' The following line prints 6.


## 範例Example

Console.WriteLine((Function(num As Integer) num + 1)(5))


## 範例Example

Dim increment1 = Function(x) x + 1
Dim increment2 = Function(x)
Return x + 2
End Function

' Write the value 2.
Console.WriteLine(increment1(1))

' Write the value 4.
Console.WriteLine(increment2(2))


## 範例Example

Lambda 運算式會在 Language-Integrated Query (LINQ)Language-Integrated Query (LINQ)中的許多查詢運算子，而且可以在以方法為基礎的查詢中明確地使用。Lambda expressions underlie many of the query operators in Language-Integrated Query (LINQ)Language-Integrated Query (LINQ), and can be used explicitly in method-based queries. 下列範例顯示一般 LINQLINQ 查詢，然後將查詢轉譯成方法格式。The following example shows a typical LINQLINQ query, followed by the translation of the query into method format.

Dim londonCusts = From cust In db.Customers
Where cust.City = "London"
Select cust

' This query is compiled to the following code:
Dim londonCusts = db.Customers.
Where(Function(cust) cust.City = "London").
Select(Function(cust) cust)