Solved

Subs vs. Functions

Posted on 2001-06-22
9
206 Views
Last Modified: 2010-05-02
I've recently inherited a VB project that has been in development for over a decade. My question concerns performance of Subs vs. Functions. All procedures are defined as Functions whether or not anything is returned.

Also Note:
No Public or Private scope set (Public By default)
No Return type (Does it default to Variant?  Is it wasting resources?)

Here is a typical Example showing the syntax:
Function SumFunction()
'do Stuff
'SumFunction is NOT set to anything
End Function

There is a significant number of these functions (100's) that are declared this way.   If I were to correct the above example, it would be like this:

Public Sub SumFunction()
'do stuff
End Sub

Is it worth my time and effort to correct these?  Is there any performance benefit to converting these functions that don't return values to Subs?

Thanks for the input,

Wes
0
Comment
Question by:wesweeks
[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
9 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 6218953
Firstly what are the scopes? There are Public and Private.

Public
======
Functions or Subs defined as Public will mean that it will be global and all modules, forms, etc will be able to use this function or sub. Use it if this function or sub is commonly used throughout the project.

Private
=======
This is the opposite of Public. If defined as Private, it can only be accessed by functions or subs in the same module or form. Functions or subs in another module or form will not be able to access it. Use this to minimize resources usage.

When to use Functions or Subs???
Use functions if you need to return something. Below is a private function that will return a boolean value.
Private Function Fun1() As Boolean
''''
    Fun1 = True ' Return true value
End Function

Use Subs when you do not need to return a value. Below is a private sub.
Private Sub procedure1()
'
End Sub


When you see something like this without Private or Public declaration, then that's means it is Public
Function Fun2()
End Function
Sub procedure2()
End Sub

If a function does not declare a return type, then that means it is of Variant type. This means ANY type. It can return a boolean, string, integer, etc. However, this will result in more memory usage in order to carter for the various numerous return type size possibilities.

hongjun
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6218954
If nothing is returned, no processing occurs.. hence you will receive no benefit from changing it to SUBs  The function above is taking the default {Public} no gain here either...  It's just a case of ugly code!, big problems when you try to convert to .net though
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6218972
If a function is declared to return a boolean but in the function it is not returning anything, it will return false. If a function is declared to return a string but in the function it is not returning anything, it will return vbNullString. And so on.... Just ugly code. Change it to Sub since it is not return anything.

hongjun
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 1

Expert Comment

by:morgan_peat
ID: 6218991
Change them to Sub.
If you don't explicitly name the type of variable returned in a function, it returns a Variant (the biggest data type at around 16 bytes).
Although you will not see an increase in speed until you hit millions of method calls, every little helps.
Why bother returning anything when you don't need to?
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 50 total points
ID: 6219030
There is a slight overhead in calling a function compared to a sub.  Modifying these to be subs will give you a (probably unnoticable) speed increase, and a (possibly more significant) decrease in compiled program size.

Most likely, the original programmer was a C programmer, where everything is a function (some are just defined as VOID, which means they don't return anything).

As for scope - that was well covered in hongjun's post (above).
0
 
LVL 3

Expert Comment

by:Joebob
ID: 6219093
Below is output from Numega TrueTime.  The left column is the amount of time the operation took in milliseconds.  The right column is the code itself.  As you can see, calling Test1 (a variant function) takes 2 milliseconds longer per 1000 times being called.  Basically, there is little benefit of making the change.

     Option Explicit    
         
0.6771     Function Test1()    
54.4966       Me.Print "Hello"    
1.0134     End Function    
         
0.5872     Sub Test2()    
54.0923       Me.Print "Hello"    
0.8992     End Sub    
         
0.0072     Private Sub Form_Click()    
       Dim i As Long    
0.2376       For i = 1 To 1000    
56.1652         Test2    
       Next i    
           
0.0636       For i = 1 To 1000    
58.1320         Test1    
       Next i    
0.0004     End Sub    
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6219138
The main benefit of correcting these are your own piece of mind as you delve into the code.

We're working on a similar project started in VB3 where all non-local variables were defined as global (because that's all you had.)  I'm painstakingly converting those to either module-level or, in some cases, local because of the problems that have occurred in the program where a global variable is inadvertently changed in the wrong place.

Also, setting procedures to private will have the benefit of not having them included in the "Intellisense" list that shows up when you reference the object.

And sub vs. function:  If you know it doesn't return anything, sub is more appropriate, but I don't really see any benefit (other than those minimal time savings mentioned) to switching it at this point.
0
 

Author Comment

by:wesweeks
ID: 6219176
I got great advice from many people very quickly.  It's too bad you have to select only one person to receive the points.  Basically, the answers supported the conclusion that I had reached on my own, but I wanted to be sure.  Thanks to everyone for the great advice.

Wes
0
 
LVL 32

Expert Comment

by:bhess1
ID: 6219293
One more thing - for those functions that Do return values, you should definitely Type the returns.  A Variant uses extra memory, and extra processing time when returning a value to a typed variable
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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