Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-01-2009
Member
 
Join Date: Dec 2008
Posts: 9
Difference between Sub and Function in Visual Basic and VB.Net

What's the difference between a Sub and a Function in Visual Basic ?
Reply With Quote
  #2  
Old 21-01-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
re: Difference between Sub and Function in Visual Basic and VB.Net

A Function can return a value from itself. For example

Code:
Public Function Sum(ByVal a As Integer, _
ByVal b As Integer) As Integer
Return a + b
End Function
Then you can use this Function as follows

Dim result As Integer = Sum(2, 4)

On the other hand a Sub can not return a value from itself. It must use a
ByRef parameter to get a result. For example

Code:
Public Sub SumSub(ByVal a As Integer, _
ByVal b As Integer) As Integer)
Dim theSum As Integer = a + b
End Sub
This has no way of returning the result and using this like

Dim answer As Integer = SumSub(2, 4)

will generate a complile time error.
Reply With Quote
  #3  
Old 21-01-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Sub Vs Function in Visual Basic

The Sub statement is used to execute one or more lines of code, period.

The Function statement is used to execute one or more lines of code AND return a value to the caller.

Use a Sub when you do not need to return a value to the calling code. Use a Function when you need to return a value.
Reply With Quote
  #4  
Old 21-01-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Sub and Function in Visual Basic.Net

To learn more about Visual Basic.NET Sub and Function statements click these links:

Sub
Function
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Difference between Sub and Function in Visual Basic and VB.Net"
Thread Thread Starter Forum Replies Last Post
Difference between Visual Basic and Access Well-Tech Windows Software 4 07-09-2010 04:09 PM
Is GUI same like Visual Basic ? Caesar Software Development 2 02-03-2009 01:32 PM
Visual Basic 2005 or Visual Basic 6 Aasha Software Development 5 15-01-2009 06:56 PM
Visual Basic 2008 & VB.net Difference any? samit Software Development 4 05-08-2008 07:44 PM
Visual Basic on LAN djbbenn Software Development 2 05-08-2008 02:15 PM


All times are GMT +5.5. The time now is 04:37 AM.