Tag Archives: Data Flow

Parameters and data flow

This is always a hot topic at this time of year. It’s been a while since we did procedures, parameters, byVal, Byref & Data Flow

From Shawlands Acc

  • Data flow within a program is handled by parameters
  • A parameter is a value or a variable which is passed into or out from a subroutine
  • The parameters used in a procedure call are ACTUAL parameters
  • The parameters used in a procedure definition are called FORMAL parameters
  • A parameter is passed BY REFERENCE if it is to by passed into and back out of a procedure
  • A parameter is passed BY VALUE if it is to by passed into but NOT back out of a procedure
  • Arrays are ALWAYS passed by reference
  • The use of parameters means that variables can be kept local to subroutines
  • This leads to programs being more reliable and robust

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