Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

A VERY easy question ... I think

What, exactly, is the difference between a Sub and a Function?
0
lillockey
Asked:
lillockey
  • 2
  • 2
  • 2
  • +1
1 Solution
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now