A VERY easy question ... I think

What, exactly, is the difference between a Sub and a Function?
lillockeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DocMCommented:
The Function returns a value.
0
DocMCommented:
For example:

Private Sub Command3_Click()
 result = Add15(4)
 Debug.Print result
End Sub

Function Add15(x) As Double
'This functions returns a double-precision floating-point value

Add15 = x + 15

End Function
0
SSSoftwareCommented:
Lillockey:

A subroutine, a Function and an Event procedure are almost identiical.

1 They all contain Code that can be executed

2 They all can but do not have to recieve parameters

3 They follow the same naming conventions.
     Note: an event procedure has a very strict naming convention of ObjectName_EventName or ControlName_EventName

4) An Event procedure (subroutine) must reside in a Form module. That is a module that is associated with a Form.

5) Subroutines and Functions can reside in either a form module or a standalone module

6) A subroutine cannot be used in a conditional statement
or on the right side of an assignment statement ( = )

7) A subroutine can only be used in a standalone statement
  ie. a CALL statement

8) A function can be used on the right hand side of an assignment statement or in a conditional statement.

   It can actually be used where ever a variable can be used. With the exception that a function cannot be used to recieve (be the target of) data

    Function = "Sam"   is not valid

9) a function returns one and only one value which then replaces the function call in the statement.


Summary:
 a) a Function returns a value
 b) a Subroutine does not

Note: do not confuse returning a value with changing the contents of a parameter. Both constructs can change the contents of a parameter.


This is probably mor than you wanted but..... I.m a teacher at heart.


Good Luck,
Ed
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

lillockeyAuthor Commented:
Thanks, Ed.

As sad as it is, I have received minimal formal training, yet have completed programs with things such as FileSystemObjects, Data Access, ... and a number of other things I can't remember off the top of my head.  As an instance, I'm creating a program that edits Empire Earth's Random Map Scripts automatically.  It's a challenge but ... since I do it for fun and not money ... it's kinda slow.   Ah well ... thank you for all of your help.

-Chris
0
nastufaCommented:
All hail the empire eath editor
0
lillockeyAuthor Commented:
btw ... I have completed what is now called Empire Earth Map Script Guide (v3.3.3).
Download it here:
http://www.missilesilo.com/tenyuusoft

It is very nice.  I don't think I'll be doing any help for it as I intended it for the little group I meet with weekly.  But other than help ... it works great.

-Chris

PS ... I'll upload the final version some time tonight (3.3.0 doesn't save the files properly :P)
0
nastufaCommented:
Its nice to know that game still has loyal supporters willing to sacrifice time to make the game better.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.