67

What are the differences between the following:

  • Private Sub
  • Private Function
  • Private Class

When should each one be used?

1

3 Answers 3

162

Private is a modifier than gives the scope of the class, sub, or function.

A sub and a function are both subroutines, or sections of code that can be called in the program. The difference between them is that a function has a return value and a sub does not.

A class is a group of code that can include subs, functions, and other stuff.

4
  • 1
    Does a function have to have a return value? Or could you have a void function?
    – trysis
    Aug 6, 2014 at 20:03
  • 1
    If you don't declare a type for a function (or declare "as object") and don't specify a return value, it will return "Nothing". If you don't return a value for a function, it will be the same as returning an undefined variable.
    – xpda
    Aug 9, 2014 at 15:41
  • 1
    Is this really the only difference ? I mean in this case I don't understand why subs exist...Isn't there maybe a difference in term of memory usage or speed of the code ?
    – Seb
    Jan 16, 2017 at 15:55
  • I think it's legacy. Function and Subroutine have been around at least since Fortran 66, when all Basic had was Gosub. Usage was more strict then. In more modern languages it is usually possible to ignore the return value of a function or write a function without a return value, although it may not be the best practice. There's not much speed difference in the two -- you can try it out using a StopWatch and making a bunch of calls each to an equivalent function and sub.
    – xpda
    Jan 16, 2017 at 19:16
7

Sub is like a function but it doesnt returns any values it just executes a proccess

0

Class is a Class, Sub and Function are methods, private is an access modifier

now check this link,

http://msdn.microsoft.com/en-us/library/ms973814.aspx

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.