Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Sub and Function in VB6?

Tags:

vb6

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) As Integer   Private Sub cmdCustomerList_Click() 

What's the difference?

like image 550
CodeBlue Avatar asked Apr 13 '12 13:04

CodeBlue


People also ask

What is the difference between function and sub procedure?

A subprocedure is called, whenever it is required to perform certain tasks. It returns control to the calling code after performing a task. A function is called, whenever a value is required to be returned to the calling code after performing a task. A subprocedure does not return a value to the calling code.

What is sub in vb6?

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.

What is function vb6?

Function is a series of statements enclosed by "Function" and "End Function" statements. The Function performs an activity and returns control to the caller. When it returns control, it also returns a value to the calling code. You can define a Function in a Class, Structure & Module. By default It is Public.

How do you define a sub or a function in VBA?

Like a Function procedure, a Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments. However, unlike a Function procedure, which returns a value, a Sub procedure can't be used in an expression.


2 Answers

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

like image 66
LeleDumbo Avatar answered Oct 12 '22 08:10

LeleDumbo


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 
like image 34
D. Kermott Avatar answered Oct 12 '22 09:10

D. Kermott