?
Solved

A VERY easy question ... I think

Posted on 2003-03-08
7
Medium Priority
?
174 Views
Last Modified: 2012-05-04
What, exactly, is the difference between a Sub and a Function?
0
Comment
Question by:lillockey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:DocM
ID: 8095446
The Function returns a value.
0
 
LVL 3

Expert Comment

by:DocM
ID: 8095477
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
 
LVL 4

Accepted Solution

by:
SSSoftware earned 160 total points
ID: 8095556
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lillockey
ID: 8096742
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
 

Expert Comment

by:nastufa
ID: 12849129
All hail the empire eath editor
0
 

Author Comment

by:lillockey
ID: 12850189
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
 

Expert Comment

by:nastufa
ID: 12863617
Its nice to know that game still has loyal supporters willing to sacrifice time to make the game better.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question