General discussion

  • Creator
    Topic
  • #2115038

    Visual Basic

    Locked

    by agipson ·

    I’m currently reading “Visual Basic 6 How to Program” by Deitel & Deitel which is I found to a very good book.
    But I’m getting confuse on the Logic flow of Sub Procedures and Function Procedures.
    I know someone out there can explain the Logic of the above mention.

All Comments

  • Author
    Replies
    • #3824664

      Visual Basic

      by kevin.riggs ·

      In reply to Visual Basic

      I’m not sure what you mean by “logic flow” but the difference between a Sub and a Function is that a Sub performs some activity but does not return anything to the calling procedure. A Function returns something (say a boolean, True, or an integer,1).

      You might use a Sub to affect some data but don’t need to return any extra data to the calling procedure:

      iBeforeFilter = rsRecordset.RecordCount
      Call x(rsRecordset)
      iAfterFilter = rsRecordset.RecordCount

      Private Sub x(ByRef rsX as ADODB.RecordSet)
      rsX.filter = “fieldNumber1=’myNewFilter'”
      End Sub

      In this example you would have applied a filter to a recordset and since the recordset is passed by reference, the filter is carried back into the calling procedure so that iBeforeFilter may be 100 records and iAfterFilter may be 50 records (depending on the filter). This procedure didn’t return any values but it did interact with data.

      A Function might be:

      iBeforeFilter = rsRecordset.RecordCount
      bFilterRecordset = Call x(rsRecordset)
      If bFilterRecordset = True Then
      iAfterFilter = rsRecordset.RecordCount
      Else
      Err.Raise blah, blah, blah
      End If

      Private Function x(ByRef rsX as ADODB.RecordSet) As Boolean

      On Error GoTo ErrHandler
      Dim bPassed as Boolean
      bPassed = False
      rsX.filter = “fieldNumber1=’myNewFilter'”
      Exit Function
      ErrHandler:
      bPassed = False
      End Function

      This way we have returned a value (either True or False) and that allows us to test whether the Function x() completed successfully or not.

    • #3824433

      Visual Basic

      by lo ·

      In reply to Visual Basic

      Hi – Not sure what you mean by logic flow either. Minor comment to first answer. Both Functions and Subroutines can pass data back to the cllaing module. A function can do in two manners, subroutine only one. Get into some religious wars about if one should or not. Passed arguments can be modified by the function or subroutine. If you do CALL SUBRNAME (STATUS, ARG1, ARG2) and SUBRNAME has … STATUS = “SUCCESS” …, then except for syntax it is pretty much like a Function (STATUS = SUBRNAME (ARG1, ARG2).

      The major difference is in usage. A function can be use in ways not allowed by a subroutine (calling for a subroutine is CALL NAME (ARGUMENTS). Functions can get fancy, If (FUNCTION1(Arg1,Arg2) .ne. FUNCTION2 (Arg3,Arg4) then … is legal. If Function1 and 2 were changed to Sugroutines, then one would code CALL SUBROUTINE1 (Status1, Arg1, Arg2) CALL SUBROUTINE1 (Status2, Arg3, Arg4) If Status1 .ne. Status2) then … Functions ALWAYS return a status (integer, string, etc.)as part of the call. Some languages allow one to CALL FUNCTION1 (Arg1, Arg2), effectively turning it into a Sugroutine (the status is still returned, discarded by the compiler). Check your table of contents or index for … Difference between Functions / Subroutines. Most have a section that does a better job of discussing the two than this but … lo

    • #3821247

      Visual Basic

      by limbo ·

      In reply to Visual Basic

      Two very verbose descriptions…I hope it hasn’t confused the issue for you 🙂

      To summarise:

      When a Sub Procedure is used it can pass and return variables.

      i.e.

      Sub PlotGraph1()
      Sub PlotGraph3() as Boolean
      Sub PlotGraph3(x as Integer) as Boolean

      A Function on the other hand will ALWAYS return a variable

      i.e.

      Function PlotGraph4() as Boolean
      Function PlotGraph5(x as Integer) as Boolean

      Which one do you use if both act in roughly the same fashion?

      Well classically a Function would be use for Math, String Manipulation, Status Checking, and so on. Keep them small and make them perform one task only (fully encapsulated).

      A Sub Procedure on the other hand makes up the core or blood vessels of the program, bringing it life. So like blood vessels it should be networked to allow smooth and efficient flow with as few branches as possible.

      Both are called ‘Methods’ and this describes doing some physical execution or calculation.
      Think of it as orders or calling down into the program

      Another example of a method is an ‘Event’. This allows the same variables to be passed as a Sub Procedure. When you study this, think of it as listening or echoes coming back out from the program.

      Hmm… I’ve just realised my description is also verbose, so I’ll stop now.

    • #3821188

      Visual Basic

      by bigheart ·

      In reply to Visual Basic

      you are asking about how a program flow goes when ever we call a Sub Procedures and Function Procedures.
      Sub Procedures : This is block of code which can repeat in your program, so we write it under a Sub Procedures, so that we can call that sub procedures again and again from any where in the program for example check this out.
      Public Sub Add(Integer i, Integer j)
      Dim k as Integer
      k = i + j
      MsgBox (“Addition of Two Numbers is : ” & k)
      End Sub
      Public Sub Command1_Click()
      Call Add(10, 20)
      End Sub
      The above example is having a Sub Procedure for Command Button, when you click on it will call another Sub Procedure named “Add”, in which it is passing two values as parameters, Then inside the Sub Procedure “Add” it is adding the value and displaying it in a message box.
      Function Procedures : This is block of code which can repeat in your program, so we write it under a Sub Procedures, so that we can call that sub procedures again and again from any where in the program for example check this out.
      Public Function Add(Integer i, Integer j) as Integer
      Dim k as Integer
      k = i + j
      Add = k
      End Sub
      Public Sub Command1_Click()
      Dim result as Integer
      result = Add(10, 20)
      Msgbox (“The return from the Function Add() is : ” & result)
      End Sub
      The above example is having a Function Procedure for Command Button, when you click on it will call another Function Procedure named “Add”, in which it is passing two values as parameters, also a return value from the Function Procedure has to be given for example in this it is given as Integer, Then inside the Function Procedure “Add” it is adding the value and then it return’s the value to the varaiable against which this Function is called, so the program flow again goes back to the called procedure.

    • #3796296

      Visual Basic

      by agipson ·

      In reply to Visual Basic

      This question was closed by the author

Viewing 4 reply threads