Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with no Return - How does this function work?

Tags:

vb.net

I'm working on a old VB project and I'm rewriting it into C#.

I've got a few lines that do not fit on what I know about functions. I've read MSDN and as far as I've understood it, a function must have a Return or an Exit, or if not (Does not makes sense having procedures) you can send the data ByRef.

This function does not have either of these, but still it's supposed to be doing something - if not I don't think the program would work, but it does.

Also it looks like it's calling itself but with an overload; but I don't know, it's too odd for me.

The function itself:

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

The variable factor is defined somewhere else in the code and it's:

double Factor = 1;

The calls to that function:

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
like image 960
Elan Sanchez Avatar asked Sep 14 '26 00:09

Elan Sanchez


2 Answers

In Visual Basic, the name of the function itself becomes a variable you can use within the function. At the end of the function, whatever is left in it is used as the return value, unless an explicit Return statement is used.

In your case, f is used throughout.

Quoting 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.

like image 119
James Thorpe Avatar answered Sep 16 '26 22:09

James Thorpe


f = Replace(f, ",", ".")

Name of your function is f and this local variable is your returned value (because name is the same). Is works automatically.

like image 32
user2216 Avatar answered Sep 16 '26 22:09

user2216



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!