Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Example Code

 

Subroutines

 

Module Module1

    Sub Main()

        Dim x, y As Integer

        Console.WriteLine("Please enter the first integer")

        x = Console.ReadLine

        Console.WriteLine("Please enter the second integer")

        y = Console.ReadLine

        MultiplyNumbers(x, y)

        AddNumbers(x, y)

    End Sub

    Sub MultiplyNumbers(ByVal x, ByVal y)

        Dim Product As Integer = x * y

        Console.WriteLine("The product is " & Product)

    End Sub

    Sub AddNumbers(ByVal x, ByVal y)

        Console.WriteLine("The sum is " & x + y)

    End Sub

 

End Module

 

 

Functions

 

Module Module1

    Sub Main()

        Dim x, y As Integer

        Dim Product As Integer

        Dim Sum As Integer

        Console.WriteLine("Please enter the first integer")

        x = Console.ReadLine

        Console.WriteLine("Please enter the second integer")

        y = Console.ReadLine

        Product = MultiplyNumbers(x, y)

        Sum = AddNumbers(x, y)

        Console.WriteLine("The product is " & Product)

        Console.WriteLine("The sum is " & Sum)

    End Sub

    Function MultiplyNumbers(ByVal x As Integer, ByVal y As Integer) As Integer

        Dim Multiply As Integer = x * y

        Return Multiply

    End Function

    Function AddNumbers(ByVal z As Integer, ByVal v As Integer) As Integer

        Dim theSum As Integer

        theSum = z + v

        Return theSum

    End Function

 

End Module

 

 

 

Random Numbers Example 1

 

Module Module1

    Sub Main()

        Dim randomizer As New Random()

        Dim i As Integer

        Console.WriteLine("The largest integer is " & Int32.MaxValue)

         Console.WriteLine()

        Console.WriteLine("5 random integers")

         Console.WriteLine()

        For i = 1 To 5

            Dim numbers1 As Integer = randomizer.Next()

            Console.WriteLine(numbers1)

        Next

        Console.WriteLine()

       Console.WriteLine("5 Random Doubles")

        Console.WriteLine()

        For i = 1 To 5

           Dim numbers2 As Double = randomizer.NextDouble()

            Console.WriteLine(numbers2)

        Next

        Console.WriteLine()

        Console.WriteLine("5 integers in the range 10 to 20")

        Console.WriteLine()

        For i = 1 To 5

           Dim numbers3 As Integer = randomizer.Next(10, 21)        'Values will be in range 10 to 20

            Console.WriteLine(numbers3)

        Next

    End Sub

 

End Module

 

 

Random Numbers Example 2

 

Module Module1


    Sub Main()
        Dim Num1, Num2, Num3 As Integer
        Num1 = GetInput()
        Num2 = GetInput()
        Num3 = GetInput()
        ListNumbers(Num1, Num2, Num3)
    End Sub
    Function GetInput()
        Dim Response As String = "No"
        While (Response <> "Yes")
            Console.WriteLine("Type Yes when you are ready for me to calculate a random number")
            Response = Console.ReadLine()     
        End While
        Return CalculateNumber()
    End Function
    Function CalculateNumber()
        Dim Randomizer As New Random()
        Return Randomizer.Next(1, 8)
    End Function
    Sub ListNumbers(ByVal Num1, ByVal Num2, ByVal Num3)
        Console.WriteLine("The numbers are: " & Num1 & " " & Num2 & " " & Num3)
    End Sub

End Module

 

ByRef and ByVal

 

Module Module1

    Sub Main()

        Dim x As Integer = 4

        Dim y As Integer = 7

        Console.WriteLine("Value of x before call to Mystery " & x)

        Console.WriteLine("Value of y before call to Mystery " & y)

        Mystery(x, y)

        Console.WriteLine("Value of x after call to Mystery " & x)

        Console.WriteLine("Value of y after call to Mystery " & y)

    End Sub

    Sub Mystery(ByVal a As Integer, ByRef b As Integer)

        a = 2 * a

        b = 2 * b

    End Sub

End Module

 

 

 

Scope

 

The following will print a is 6

 

Module Module1

    Sub Main()

        Dim a As Integer = 6

        Example1(a)

    End Sub

    Sub Example1(ByVal a)

        Console.WriteLine("a is " & a)

    End Sub

End Module

 

The following will not run

 

Module Module1

    Sub Main()

        Dim a As Integer = 6

        Example1(a)

        Example2()

    End Sub

    Sub Example1(ByVal a)

        Console.WriteLine("a is " & a)

    End Sub

    Sub Example2()

        Console.WriteLine("a is " & a)

    End Sub

End Module


Global Variables

 

Module Module1
    Dim price As Integer = 20
    Sub Main()
        AnnouncePrice()
    End Sub
    Sub AnnouncePrice()
        Console.WriteLine("Price is: " & price)
    End Sub
End Module

 

Math Methods

 

Module1

    Sub Main()

        Dim pi As Single = 3.1416

        Dim a As Integer = -2

        Console.WriteLine("pi = " & pi)

        Console.WriteLine("a = " & a)

        Console.WriteLine("Absolute value of a is " & Math.Abs(a))

        Console.WriteLine("a raised to power 3 is " & Math.Pow(a, 3))

        Console.WriteLine("Sin of pi/4 is " & Math.Sin(pi / 4))

        Console.WriteLine("Tangent of pi/4 is " & Math.Tan(pi / 4))

        Console.WriteLine("Square root of a squared is " & Math.Sqrt(a * a))

        Console.WriteLine("Largest of pi and a is " & Math.Max(pi, a))

        Console.WriteLine("Smallest of pi and a is " & Math.Min(pi, a))

    End Sub

End Module

 

Note the small machine precision errors