r/visualbasic • • Mar 25 '20

How to print/call a variable from a function in a sub.

So I have a quadratic equation function which prints 2 variables xpos and xneg. How do I call those two in another sub.

5 Upvotes

11 comments sorted by

2

u/JasonHenley Mar 25 '20

Could you give more context? There are a few ways variables are passed around. One is by a return value from a function:

Function FooFunc() as String
    Dim bar as string = "hello world"
    'Do stuff
    Return bar
End Function

Sub Main()

    Dim baz as String
    baz = FooFunc()
    Console.WriteLine(baz)

End Sub

Here's how you pass a variable to a sub:

Sub FirstSub()
    Dim foo as string = "hello world"
    Call SecondSub(foo)
End Sub

Sub SecondSub(bar as string)
    Console.WriteLine(bar)
End Sub

Another way is by global variable but this is usually the least desirable option:

Private foo as string

Sub FooSub()
    foo = "hello world"
End Sub

Sub Main()
    Console.WriteLine(foo)
End Sub

1

u/SpecyTwat67 Mar 25 '20

So I have a quadratic function which returns two variables xpos xneg. Then I want to get these two variables in another sub to get these to a certain degree of decimals. But idk how

1

u/JasonHenley Mar 25 '20

Post some of your code so we know where exactly you have the problem and can suggest the most proper way to pass the variables.

1

u/SpecyTwat67 Mar 25 '20

https://pastebin.com/KmJ6ahQv

heres all of my code. my final task is to ask the user what degree of decimal places the they want their xpos and xneg values

1

u/JasonHenley Mar 25 '20

Okay, so from what I can read, you are calling the function named "quad" from the sub "quadEqu". Your function "quad" always returns a null string, "", which defeats the purpose of a function. Functions should return something useful. Your line Console.WriteLine(quad(a, b, c)) always writes a blank line to the screen as a result.

If you have covered creating classes or structs yet, then you should make a structure that can contain both values and return that from your function. Example:

Private Structure QuadraticSolutions
    Dim xpos as double
    Dim xneg as double
End Structure

...

Function quad(x As Double, y As Double, z As Double) As QuadraticSolutions
    'Do stuff
    Dim mySolutions as New QuadraticSolutions
    mySolutions.xpos = (-y + (Sqrt(iSqr))) / (2 * x)
    mySolutions.xneg = (-y - (Sqrt(iSqr))) / (2 * x)
    'More stuff
    Return mySolutions
End Function

'Now in your quadEqu sub, you can get mySolutions from the function like this

'...

If intCh(c) Then
                    Console.WriteLine("c = " & c)
                    Dim mySolutions as QuadraticSolutions
                    mySolutions = quad(a, b, c)

1

u/SpecyTwat67 Mar 25 '20 edited Mar 25 '20
    Private Structure quadraticsolution
        Dim xpos As Double
        Dim xneg As Double
    End Structure
   
 Sub quadEqu()
        Console.Clear()
        Dim nDec As Integer

        Console.WriteLine("How many decimal places would u like your answer in? ")
        nDec = Console.ReadLine()

        Console.WriteLine("Quadratic Equation - please enter your values induvidually starting from a")
        Dim a, b, c As String
        a = Console.ReadLine
        If intCh(a) Then
            Console.WriteLine("a = " & a)
            b = Console.ReadLine
            If intCh(b) Then
                Console.WriteLine("b = " & b)
                c = Console.ReadLine
                If intCh(c) Then
                    Console.WriteLine("c = " & c)
                    Dim mySolutions As QuadraticSolutions
                    mySolutions = quad(a, b, c)
                    Console.WriteLine(quad(a, b, c))
                Else
                    Console.WriteLine("please enter integer")
                End If
            Else
                Console.WriteLine("please enter integer")
            End If
        Else
            Console.WriteLine("please enter integer")
        End If
    End sub

    Function quadn(x As Double, y As Double, z As Double) As quadraticsolution
        Dim iSqr As Double
        iSqr = (y ^ 2 - (4 * x * z))
        'Do stuff
        Dim mySolutions As New quadraticsolution
        mySolutions.xpos = (-y + (Sqrt(iSqr))) / (2 * x)
        mySolutions.xneg = (-y - (Sqrt(iSqr))) / (2 * x)
        'More stuff
        Return mySolutions
    End Function

Severity Code Description Project File Line Suppression State

Error BC30002 Type 'QuadraticSolutions' is not defined. coursework C:\Users\M\source\repos\coursework\coursework.vb 77 Active

Error BC30508 'quadn' cannot expose type 'coursework.quadraticsolution' in namespace 'coursework' through module 'coursework'. coursework C:\Users\M\source\repos\coursework\coursework.vb 207 Active

i get these two errors

1

u/JasonHenley Mar 26 '20

I sure hope you're using Visual Studio, because it will underline where your errors are, and there's an error list tab at the bottom of the IDE. I can already tell what the problem is -- your structure is named quadraticsolution, but you have

Dim mySolutions as QuadraticSolutions

with an 's' at the end.

1

u/SpecyTwat67 Mar 26 '20

ructure as a return value, you could pass the xpos and xneg by reference

i am using visual studio i have fixed one error but the other error

Severity Code Description Project File Line Suppression State

Error	BC30508	'quadn' cannot expose type 'Module1.quadraticsolution' in namespace 'test' through module 'Module1'.	test	C:\Users\Mohsin\source\repos\test\Module1.vb	48	Active

still persists

1

u/JasonHenley Mar 27 '20

Ah, sorry, my fault. You need your structure to be Friend or Public, not Private.

Friend Structure quadraticsolution
         Dim xpos As Double
         Dim xneg As Double
End Structure

1

u/f15sim Mar 26 '20

Instead of defining a structure as a return value, you could pass the xpos and xneg by reference.

Look at the help for the ByRef keyword. It allows you to build a function (or subroutine for that matter) that "returns" more than one value.

1

u/teamhog Mar 25 '20

Make them global or pass them into the new sub.

VBA Global Variable

Passing values to a Sub