Friday, July 31, 2009

What is the difference between a function and a subroutine in visual basic?

Support your answer with a written program if possible.

What is the difference between a function and a subroutine in visual basic?
A function returns a value. A Subroutine does not (void in C#)
Reply:Functios and subroutine both are functions used in visual basic but single difference in between them is function can return value while subrouthin cannt
Reply:In almost all languages that have both procedures (subroutines) and functions, the difference is that functions return values.





Have a look in your help files for many examples of both.





Rawlyn.
Reply:None of the above answers are complete, actually.





In VB, a Function is indeed a method that returns a value, but it's important to specify that it returns a value by *assignment* (across the equals sign):





Private Function add(ByVal num1 as Integer, ByVal num2 as Integer) as Integer





answer = add(number1,number2)





An independent sub procedure (as they're called in VB) does not


have a return value by assignment. It CAN, however, return a value by *reference*:





private sub add(ByVal num1 as Integer, ByVal num2 as integer, ByRef sum as Integer)





add(number1, number2, answer)





The variable "answer" is assigned the appropriate value in both cases, it's just HOW that happens that is different.





Hope that helps :)





Rob

rose garden

No comments:

Post a Comment