慕莱坞7535251
只是将返回值设置为函数名仍然与Java(或其他)语句不完全相同return,因为在java中,return退出函数,如下所示:public int test(int x) {
if (x == 1) {
return 1; // exits immediately
}
// still here? return 0 as default.
return 0;}在VB中,如果未在函数末尾设置返回值,则精确等效项需要两行。因此,在VB中,确切的推论看起来像这样: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既然如此,那么知道你可以像使用方法中的任何其他变量一样使用return变量也是很好的。像这样:Public Function test(ByVal x As Integer) As Integer
test = x '
If test <> 1 Then ' Test the currently set return value
test = 0 ' Reset the return value to a *new* value
End IfEnd Function或者,返回变量如何工作的极端例子(但不一定是你应该如何实际编码的一个很好的例子) - 那个会让你夜不能寐的一个例子:Public Function test(ByVal x As Integer) As Integer
test = x '
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 IfEnd Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)