Returning values from functions in structures in Visual Basic

Arcturus

I hope the title of the post isn't too much of a mess. I'm reviewing some course material from last week, and there is just one thing I don't understand in regard to this particular structure and the add and subtract functions in it:

  Structure ComNum
    Dim Re As Double
    Dim Im As Double
    Function add(ByVal br As ComNum) As ComNum
        add.Re = br.Re + Re
        add.Im = br.Im + Im
    End Function
    Function subt(ByVal br As ComNum) As ComNum
        subt.Re = br.Re - Re
        subt.Im = br.Im - Im
    End Function
End Structure

Sub Main()
    Dim a, b, c As ComNum

    a.Re = 2
    a.Im = 3
    b.Re = 4
    b.Im = 5

    c = a.add(b).add(b).subt(b)

    Console.WriteLine("The second number added twice and subtracted once from the first number gives {0}+{1}i", c.Re, c.Im)

End Sub

Now, the way I understand functions is that once anything is returned from it, execution of the function stops at that exact line where the value is returned and nothing after it gets executed. According to that, it should add the real part and exit the function.

I know I'm missing a key thing here and I'd appreciate it if someone could explain this to me.

Nathan Champion

The way you have this setup those functions are creating a new, empty ComNum structure each time you call them (named either add or subt based on the function name). Unless you manually return them early it will just default to returning the function named structure.

    Function add(ByVal br As ComNum) As ComNum
        add.Re = br.Re + Re
        add.Im = br.Im + Im
    End Function

Is basically doing the equivalent of:

    Dim add As New ComNum
    add.Re = br.Re + Re
    add.Im = br.Im + Im
    Return add

Though like Lars pointed out I'm not sure why you'd want this to be a function vs a sub. Using it the way you have it setup now requires you to do something like this to get the add/subtract values because you need to capture the returned ComNum object.

    Dim a As New ComNum With {.Im = 1, .Re = 1}
    'Im = 6, Re = 6
    a = a.add(New ComNum With {.Im = 5, .Re = 5}) 

Doing something like this makes more sense to me.

Structure ComNum
    Dim Re As Double
    Dim Im As Double
    Sub add(ByVal br As ComNum)
        Re += br.Re
        Im += br.Im
    End Sub
    Sub subt(ByVal br As ComNum)
        Re -= br.Re
        Im -= br.Im
    End Sub
End Structure

Then you could just call it this way to update the struct without having to capture the returned values.

a.add(New ComNum With {.Im = 5, .Re = 5}) 

Edit: Knowing more now how the exercise is supposed to be performed I'd suggest something like this for the struct:

    Public Overrides Function ToString() As String
        Return String.Format("Re: {0}  Im: {1}", Re, Im)
    End Function

Then you could call the .ToString() method like this. Though, just a thought.

    Console.WriteLine("The second number added twice and subtracted once from the first number gives {0}", c.ToString())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Returning values from functions in Python

returning multiple values from functions

returning values from decorated functions in python

Returning values from functions when efficiency matters

Returning values from nested functions - JavaScript

Functions and returning values

Returning values to other functions

Using Functions in Visual Basic

Confusion regarding returning large values from functions and move

method or design pattern for returning values from callback functions

How to return variable itself from a PHP functions intead of returning the values?

Returning functions from functions in JavaScript

Understanding returning values functions C

trig functions returning incorrect values

python functions not returning their values properly?

Dependency graph for functions in Visual Basic

Returning values from dictionary

Returning Values From Methods

Using a function without returning a value visual basic

Visual Basic Or, OrElse still returning false

Valid F# functions for returning anonymous records with different structures

Returning from /bin/sh Functions

Passing structures in functions when the structure comes from an "array of structures"

What could be the good way to check if N optionals returning from different functions all have values

Error returning values in multiple callback functions with electron

Passing labels to selectize and returning values for server functions

Two matrix summation functions returning different values

Folding/Collapsing functions and subroutines in Visual Basic 6

Old visual basic mid and left functions