What is the difference between Sub and Function in VB6?

前端 未结 8 1852
孤城傲影
孤城傲影 2020-12-25 09:57

I am going through some old VB code and I run into function definitions like these -

 Private Function ExistingCustomer(Index As Integer, Customer As String)         


        
相关标签:
8条回答
  • 2020-12-25 10:22

    function in vb

    • a function must return some value/s
    • Syntax : private function fun_name(argument/s(optional)) as return_type(integer,string..) return value end function
    • fun_name(arguments(optional) ) is enough for function call

    sub in vb

    • a sub need not to be return any value/s
    • Syntax : private sub sub_name(argument/s(optional))

      end sub

    • sub_name(arguments(optional) ) is enough for function call
    0 讨论(0)
  • 2020-12-25 10:23

    Function returns value, Sub doesn't. It's that simple.

    0 讨论(0)
  • 2020-12-25 10:36
    1. Syntax of functions will be Function...End function and for Sub will be Sub...End Sub.
    2. Functions may or may not have objects but sub doesn't have objects
    3. Functions are re-usable where Sub doesn't
    4. Functions can return values but sub doesn't
    5. Functions may have object repository but sub doesn't
    6. Extension of functions is .qfl where for sub it's .vba
    0 讨论(0)
  • 2020-12-25 10:38

    A function can also be used in an expression. A Subroutine cannot. Functions can lend to the readability of your code better than a subroutine.

    Here's an example of how a function can increase readability:

    If AccountIsLocked("JJones") then Msgbox("This account is locked")
    

    this function would be defined somewhere

    public function AccountIsLocked(UserId as string) as boolean
       dim usr = uow.AccountRepository.UserInfo(UserId)
       return usr.locked
    end function
    

    Here's the same example but coded with a subroutine:

    CheckIfAccountLocked("JJones")
    

    and elsewhere this sub is defined:

    public sub CheckIfAccountLocked(UserId)
           if uow.AccountRepository.UserInfo(UserId).locked then
              msgbox("Account is locked")
           end if
    end sub
    

    Also note that checking the value is separated from the action -- this contributes to separation of duties. The function would lend toward re-usability.

    With VB6 there are some odd rules governing parenthesis. If there are no parameters to a sub then the parenthesis are not needed (I think Visual Studio might remove the parenthesis). One way around this is to add the keyword "Call" before your sub.

    Call CheckIfAccountLocked()
    

    vs

    CheckIfAccountLocked
    
    0 讨论(0)
  • 2020-12-25 10:38

    What is the difference between Sub and Function in VB6?

    "sub" can perform some action. "sub" returns no value.

    Example:

    Form_Load()

    "function" can also perform some action but it also returns some value to point from which it was called. that is, "Functions return a value, often based on a variable"

    Example:

    Val(), FormatPercentage().

    0 讨论(0)
  • 2020-12-25 10:39

    They are both sections to write code however a function must return a value. For example if you had a program in which a complicated mathematical procedure needs to be executed a number of times you would simply make a function and have the complicated maths code in there and any time you need to do the calculation you can just call the function. Hope this helped not sure if I explained it well.

    0 讨论(0)
提交回复
热议问题