Procedimentos de função (Visual Basic)
A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code.
Each time the procedure is called, its statements run, starting with the first executable statement after the Function statement and ending with the first End Function, Exit Function, or Return statement encountered.
You can define a Function procedure in a module, class, or structure. It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it.
A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.
Declaration Syntax
The syntax for declaring a Function procedure is as follows:
[modificadores de] Function nomedafunção[(parameterlist)] As returntype
' Statements of the Function procedure.
End Function
The modifiers can specify access level and information regarding overloading, overriding, sharing, and shadowing. For more information, see Instrução Function (Visual Basic).
You declare each parameter the same way you do for Subprocedimentos (Visual Basic).
Data Type
Every Function procedure has a data type, just as every variable does. This data type is specified by the As clause in the Function statement, and it determines the data type of the value the function returns to the calling code. The following sample declarations illustrate this.
Function yesterday() As Date
End Function
Function findSqrt(ByVal radicand As Single) As Single
End Function
For more information, see "Parts" in Instrução Function (Visual Basic).
Returning Values
The value a Function procedure sends back to the calling code is called its return value. The procedure returns this value in one of two ways:
It assigns a value to its own function name in one or more statements of the procedure. Control does not return to the calling program until an Exit Function or End Function statement is executed. The following example illustrates this.
Function nomedafunção[(parameterlist)] As returntype
' The following statement does not transfer control back to the calling code.
nomedafunção = expressão
' When control returns to the calling code, expressão is the return value.
End Function
It uses the Return statement to specify the return value, and returns control immediately to the calling program. The following example illustrates this.
Function nomedafunção[(parameterlist)] As returntype
' The following statement immediately transfers control back to the calling code and returns the value of expressão.
Return expressão
End Function
The advantage of assigning the return value to the function name is that control does not return from the procedure until it encounters an Exit Function or End Function statement. This allows you to assign a preliminary value and adjust it later if necessary.
For more information, see "Return Value" in Instrução Function (Visual Basic).
Returning Arrays
If the Function procedure returns an array data type, you cannot access the individual elements of the array within the function. If you attempt to do this, the compiler interprets it as a recursive call to the procedure. The following example illustrates this.
Function allOnes(ByVal n As Integer) As Integer()
For i As Integer = 1 To n - 1
' The following statement generates a COMPILER ERROR.
allOnes(i) = 1
Next i
' The following statement generates a COMPILER ERROR.
Return allOnes()
End Function
In the preceding example, the compiler interprets the attempted assignment allOnes(i) = 1 as a call to allOnes on the left side of an assignment statement. It interprets the attempted Return allOnes() as a call with no argument. Both statements generate compiler errors.
Calling Syntax
You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses.
The syntax for a call to a Function procedure is as follows:
lvalue = nomedafunção[(Listadeargumentos)]
If ((nomedafunção[(Listadeargumentos)] / 3) <= expressão) Then
When you call a Function procedure, you do not have to use its return value. Se você não fizer isso, todas as ações da função são executadas, mas o valor retornado será ignorado. MsgBoxé freqüentemente chamada dessa maneira.
Illustration of Declaration and Call
The following Function procedure calculates the longest side, or hypotenuse, of a right triangle, given the values for the other two sides.
Function hypotenuse(ByVal side1 As Single, ByVal side2 As Single) As Single
Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2))
End Function
The following example shows a typical call to hypotenuse.
Dim testLength, testHypotenuse As Single
testHypotenuse = hypotenuse(testLength, 10.7)
Visual Basic Runtime Functions
The Visual Basic runtime provides many functions, located in the Microsoft.VisualBasic namespace. Eles incluem funções comuns como o Beep, MsgBox, e StrComp funções. You call these functions the same way you call your own Function procedures.
Consulte também
Tarefas
Como: Criar um procedimento que retorna um valor (Visual Basic)
Como: Retornar um valor de um procedimento (Visual Basic)
Como: Chamar um procedimento que retorna um valor (Visual Basic)
Referência
Instrução Function (Visual Basic)
Conceitos
Subprocedimentos (Visual Basic)
Procedimentos de propriedade (Visual Basic)