Data Flow in Programs

There is a lot of confusion over input and output (ByVal/ByRef).

Consider the following example.

Write a program using procedures that gets 2 numbers, adds them together before displaying the answer.

Structure diagram

Algorithm

1. Get 2 numbers

    Out NumA, NumB

    2. Add the numbers together

      In NumA, NumB

      Out Sum

      3. Display the answer

      In Sum

      Code

      Private Sub Main()

      Dim NumA as Integer

      Dim NumB as Integer

      Dim NumSum as Integer

      Call GetTwoNumbers(NumA,NumB)

      Call AddTwoNumbers(NumA,Numb,NumSum)

      Call DisplayAnswer(NumSum)

      End Sub

      Private Sub GetTwoNumbers(ByRef NumA as Integer, ByRef NumB as Integer)

      NumA=inputbox(“Please enter the first number”)

      NumB=inputbox(“Please enter the second number”)

      End Sub

      Private Sub AddTwoNumbers(ByVal NumA as Integer, byVal NumB as Integer, ByRef NumSum as Integer)

      NumSum=NumA+NumB

      End Sub

      Private Sub DisplayAnswer(ByVal NumSum as Integer)

      Msgbox(“The sum of the two numbers is “ & NumSum)

      End Sub