Compartilhar via


Como: Chamar um procedimento que retorna um valor (Visual Basic)

A Function procedure returns a value to the calling code. You call it by including its name and arguments either on the right side of an assignment statement or in an expression.

You can use the Call keyword to call a Function procedure. However, this technique ignores the value returned by the procedure.

To call a Function procedure within an expression

  1. Use the Function procedure name the same way you would use a variable. You can use a Function procedure call anywhere you can use a variable or constant in an expression.

  2. Follow the procedure name with parentheses to enclose the argument list. If there are no arguments, you can optionally omit the parentheses. However, using the parentheses makes your code easier to read.

  3. Place the arguments in the argument list within the parentheses, separated by commas. Be sure you supply the arguments in the same order that the Function procedure defines the corresponding parameters.

    Alternatively, you can pass one or more arguments by name. For more information, see Passagem de argumentos por posição e nome (Visual Basic).

  4. The value returned from the procedure participates in the expression just as the value of a variable or constant would.

To call a Function procedure in an assignment statement

  1. Use the Function procedure name following the equal (=) sign in the assignment statement.

  2. Follow the procedure name with parentheses to enclose the argument list. If there are no arguments, you can optionally omit the parentheses. However, using the parentheses makes your code easier to read.

  3. Place the arguments in the argument list within the parentheses, separated by commas. Be sure you supply the arguments in the same order that the Function procedure defines the corresponding parameters, unless you are passing them by name.

  4. The value returned from the procedure is stored in the variable or property on the left side of the assignment statement.

Exemplo

O exemplo a seguir chama o Visual Basic Environ para recuperar o valor de umavariávelde ambientede sistema operacional. As chamadas de primeira linha Environ dentro de uma expressão e a segunda linha chama em uma instrução de atribuição. EnvironObtém o nome de variável como seu único argumento. It returns the variable's value to the calling code.

MsgBox("Value of PATH is " & Environ("PATH"))
Dim currentPath As String = Environ("PATH")

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 não retorna um valor (Visual Basic)

Referência

Instrução Function (Visual Basic)

Conceitos

Procedimentos de função (Visual Basic)

Parâmetros e argumentos de procedimento (Visual Basic)