当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 没有返回的功能 – 这个功能如何工作?

来源:互联网 收集:自由互联 发布时间:2021-06-24
我正在研究一个旧的VB项目,我正在将其重写为C#. 我有几行不符合我对功能的了解.我已经阅读过MSDN,据我所知,函数必须有一个返回或退出,或者如果没有(有程序没有意义)你可以发送数据
我正在研究一个旧的VB项目,我正在将其重写为C#.

我有几行不符合我对功能的了解.我已经阅读过MSDN,据我所知,函数必须有一个返回或退出,或者如果没有(有程序没有意义)你可以发送数据ByRef.

这个函数没有这些,但它仍然应该做某事 – 如果不是我不认为程序会起作用,但确实如此.

它看起来像是在调用自己但是过载;但我不知道,这对我来说太奇怪了.

功能本身:

Private Function f(ByVal numero As Double) As String
    Dim intermedio As Double
    intermedio = numero * Factor 
    If intermedio - Int(intermedio) = 0 Then

        f = Format(intermedio, "0")
    Else
        f = Format(intermedio, "###0.0######")
    End If
    'f = Format(intermedio, "0.00000E+000")
    f = Replace(f, ",", ".")
    'f = Str(numero)
End Function

变量因子在代码中的其他位置定义,它是:

double Factor = 1;

对该函数的调用:

For Y = 0 To MaxY - 1
    X0 = X
    X1 = X + 1
    Y0 = Y
    Y1 = Y + 1
    Z00 = a(X0, Y0)
    Z01 = a(X0, Y1)
    Z10 = a(X1, Y0)
    Z11 = a(X1, Y1)
    Normal(X1 - X0, Y1 - Y0, Z11 - Z00, X1 - X0, 0, Z10 - Z00)

    objWriter.WriteLine("  facet normal " & f(i) & " " & f(j) & " " & f(k))
    objWriter.WriteLine("    outer loop")
    objWriter.WriteLine("      vertex " & f(X) & " " & f(Y) & " " & f(a(X, Y)))
    objWriter.WriteLine("      vertex " & f(X + 1) & " " & f(Y + 1) & " " & f(a(X + 1, Y + 1)))
    objWriter.WriteLine("      vertex " & f(X + 1) & " " & f(Y) & " " & f(a(X + 1, Y)))
    objWriter.WriteLine("    endloop")
    objWriter.WriteLine("  endfacet")

    Normal(0, Y1 - Y0, Z01 - Z00, X1 - X0, Y1 - Y0, Z11 - Z00)

    objWriter.WriteLine("  facet normal " & f(i) & " " & f(j) & " " & f(k))
    objWriter.WriteLine("    outer loop")
    objWriter.WriteLine("      vertex " & f(X) & " " & f(Y) & " " & f(a(X, Y)))
    objWriter.WriteLine("      vertex " & f(X) & " " & f(Y + 1) & " " & f(a(X, Y + 1)))
    objWriter.WriteLine("      vertex " & f(X + 1) & " " & f(Y + 1) & " " & f(a(X + 1, Y + 1)))
    objWriter.WriteLine("    endloop")
    objWriter.WriteLine("  endfacet")

Next Y
在Visual Basic中,函数本身的名称将成为您可以在函数中使用的变量.在函数结束时,除非使用显式的Return语句,否则将其中的任何内容用作返回值.

在您的情况下,f始终使用.

引用MSDN:

The Exit Function and Return statements cause an immediate exit from a Function procedure. Any number of Exit Function and Return statements can appear anywhere in the procedure, and you can mix Exit Function and Return statements.

If you use Exit Function without assigning a value to name, the procedure returns the default value for the data type that’s specified in returntype. If returntype isn’t specified, the procedure returns Nothing, which is the default value for Object.

网友评论