-1

I cant understand about the difference between Function and Sub procedures in VB.NET.

The one with Function :

Private Function remainder (intno1 As Integer, intno2 As _ Integer) As Integer   

Dim intresult As Integer

intresult = intno1 Mod intno2      
remainder = intresult 

End Function

and then by this way i call it :

Private Sub cmdrem _Click()
Dim intm  As Integer, intn As Integer     
Dim intmod  As Integer 
intm = Val (txtno1.Text) 
intn = Val (txtno2.Text) 
intmod = remainder (intm, intn) 
lblres.text = "Answer Is = " + Str(intmod)
End Sub

would you please help me ?

dsaf
  • 27
  • 2
  • 4
  • 5
    This is a duplicate of: http://stackoverflow.com/questions/10141708/what-is-the-difference-between-sub-and-function-in-vb6 – Bugs Jun 30 '16 at 09:21
  • 2
    This doesn't appear to be VB.Net (the version of Visual Basic in Visual Studio 2002 and later). It seems to be VB6 (Visual Studio 1998 or earlier) or VBA (the version of Visual Basic used in Microsoft Office). These are all different languages. Please edit your question to use the correct tag. – Blackwood Jun 30 '16 at 09:24
  • Does it have any difference between vb.net syntax and vb6 syntax? My tutorial is for vb6 but i code in visual studio (visual basic .net) – dsaf Jun 30 '16 at 09:28
  • 3
    If you are using VB.Net you should completely ignore material related to VB6 and find something VB.Net specific, they are quite different things. – Alex K. Jun 30 '16 at 10:20

1 Answers1

1

some main differences between procedure and function in vb are that the function returns a value but sub procedure never returns value. the return type must be defined in function declaration.

A function always is declared with keyword Function and a sub procedure is declared with keyword Sub.

Function ends with the keyword end function and procedure ends with keyword end sub.

Zahid Iqbal
  • 112
  • 7
  • What do you mean by returning value ? would you please give more info about this part of your answer : are that the function returns a value but sub procedure never returns value. thank s all – dsaf Jun 30 '16 at 09:35
  • intmod = remainder (intm, intn) in this line this function is assigning a value to intmod variable which is coming from the function. – Zahid Iqbal Jun 30 '16 at 09:37
  • in the function you assign a value to the function name remainder = intresult this value you have received in the sub procudere – Zahid Iqbal Jun 30 '16 at 09:38
  • Which of these two procedures are easy to use in VB ? @Zahid – dsaf Jun 30 '16 at 10:42
  • They are both easy to use. It just depends on what your needs are. Functions return an object (though you don't need to do anything with the object either in which case you're treating the function just like a sub). For example if you had a Function that returned a Boolean type, you could have code like `If MyBoolFunc(some_parameter) Then do_some_tasks Else do_other_tasks End If` but you could also use the same function like `MyBoolFunc(some_parameter)` all by itself if you don't care what it returned for some reason. As others have said, don't use VB6 material if you're trying to learn .Net! – topshot Jun 30 '16 at 16:04