Come si restituisce un risultato da una funzione?
Per esempio:
Public Function test() As Integer
return 1
End Function
Questo dà un errore di compilazione.
Come faccio a far sì che questa funzione restituisca un intero?
Per i tipi di ritorno non-oggetto, dovete assegnare il valore al nome della vostra funzione, come questo:
Public Function test() As Integer
test = 1
End Function
Esempio di utilizzo:
Dim i As Integer
i = test()
Se la funzione restituisce un tipo di oggetto, allora devi usare la parola chiave Set
come questa:
Public Function testRange() As Range
Set testRange = Range("A1")
End Function
Esempio di utilizzo:
Dim r As Range
Set r = testRange()
Notate che assegnare un valore di ritorno al nome della funzione non termina l'esecuzione della vostra funzione. Se volete uscire dalla funzione, allora dovete dire esplicitamente Exit Function
. Per esempio:
Function test(ByVal justReturnOne As Boolean) As Integer
If justReturnOne Then
test = 1
Exit Function
End If
'more code...
test = 2
End Function
Documentazione: http://msdn.microsoft.com/en-us/library/office/gg264233%28v=office.14%29.aspx
Le funzioni VBA trattano il nome stesso della funzione come una specie di variabile. Così, invece di usare una dichiarazione "return
", direste semplicemente:
test = 1
Notate, però, che questo non esce dalla funzione. Qualsiasi codice dopo questa istruzione verrà eseguito. Così, potete avere molte istruzioni di assegnazione che assegnano diversi valori a test
, e qualunque sia il valore quando raggiungete la fine della funzione sarà il valore restituito.
Impostare semplicemente il valore di ritorno al nome della funzione non è ancora esattamente come l'istruzione Java (o altre) return
, perché in java, return
esce dalla funzione, così:
public int test(int x) {
if (x == 1) {
return 1; // exits immediately
}
// still here? return 0 as default.
return 0;
}
In VB, l'equivalente esatto richiede due righe se non si imposta il valore di ritorno alla fine della funzione. Quindi, in VB il corollario esatto sarebbe come questo:
Public Function test(ByVal x As Integer) As Integer
If x = 1 Then
test = 1 ' does not exit immediately. You must manually terminate...
Exit Function ' to exit
End If
' Still here? return 0 as default.
test = 0
' no need for an Exit Function because we're about to exit anyway.
End Function
Poiché questo è il caso, è anche bello sapere che potete usare la variabile di ritorno come qualsiasi altra variabile nel metodo. In questo modo:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test <> 1 Then ' Test the currently set return value
test = 0 ' Reset the return value to a *new* value
End If
End Function
Oppure, l'estremo esempio di come la variabile di ritorno funziona (ma non necessariamente un buon esempio di come si dovrebbe codificare) - quello che vi terrà svegli la notte:
Public Function test(ByVal x As Integer) As Integer
test = x ' <-- set the return value
If test > 0 Then
' RECURSIVE CALL...WITH THE RETURN VALUE AS AN ARGUMENT,
' AND THE RESULT RESETTING THE RETURN VALUE.
test = test(test - 1)
End If
End Function