Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Use function name as a local variable

Total Hit ( 3350)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Many programmers don't realize that it is perfectly legal to use the name of a function inside the Function itself, as if it were a local variable. This trick often lets you avoid the declaration of a temporary variable, and sometimes can speed up the code. Take for example the following code:

Click here to copy the following block
Function Max(arr() As Long) As Long
  Dim res As Long, i As Long
  res = arr(LBound(arr))
  For i = LBound(arr) + 1 To UBound(arr)
    If arr(i) > res Then res = arr(i)
  Next
  Max = res
End Function

You can make it more concise by getting rid of the res variable:

Click here to copy the following block
Function Max(arr() As Long) As Long
  Dim i As Long
  Max = arr(LBound(arr))
  For i = LBound(arr) + 1 To UBound(arr)
    If arr(i) > Max Then Max = arr(i)
  Next
End Function

You can even pass the function name by reference to another routine, or as an argument of an API function.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.