View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Sub(), Modules, and Private/ Global variables

There two types of procedures (sections of code that perform an action): Sub
procedures and Function procedure. The difference between them is that
Function procedure can return a value as the result of the function, whereas
Sub procedure do not return any value. E.g.,

Function XZY(D As Double) As Double
XYZ = D * 2
End Function

Procedure XYZ returns D * 2 as its result which can be assign to another
variable:

Dim A As Double
A = XYZ(4)

Subs don't return value. Therefore,

Sub ABC(D)
' code
End Code

is valid but

Dim A As Double
A = ABC()

is invalid because ABC can't return a value.

If you need to pass data around between procedure, you can declare variable
input parameters (e.g,. D As Double) in either a Sub or Function declaration
and those parameter values can used within the procedure. You can also
define variable at the module or project level by declaring them before and
outside of any procedure. E.g.,

Public X As Double
Sub FirstSubInModule()
' code
End Sub

Here, X will be visible throughout the project. This is refered to as the
"scope" of a variable and is document in the on line help.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"J@Y" wrote in message
...
I trying to understand is Modular programming thing. What exactly does
sub()
define? How would I create variables which can be called upon by different
modules?