Solved

Subs vs. Functions

Posted on 2001-06-22
9
201 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
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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now