Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

where to declare variables

Status
Not open for further replies.

amiw

Programmer
Apr 1, 2003
113
GB
Below is a function, where abouts in my ASP script do i declare the variable sSQLCount ie within the function or at the top of my script?

Function CountFAQ(CategoryID)
sSQLCount = "SELECT Count(*) From tblFAQ Where CategoryID =" & CategoryID & " AND Active='Y'"

Set oRecordsetCount = Server.CreateObject("ADODB.Recordset")

oRecordsetCount.Open sSQLCount, oConnection
oRecordsetCount.MoveFirst

NumberFAQ = oRecordsetCount(0)
CountFAQ = NumberFAQ

oRecordsetCount.Close
Set oRecordsetCount=Nothing
End Function


 
If the variable is only going to be used inside the function then in most languages you would be beyter of declaring it inside the function. What this does is cause the system to release the memory for the variable after the function has completed processing. Variables generally remain as memory handles until their scope has completed execution, so here you have the choice between function scope or page scope (to put it into pseudo-web terms).
If you don't need that variable outside the function, then best guess would be to define it only in the function and then allow the system to deinstantiate it (and release the memory) when the function is done processing it.

A lesser concern might be efficiency. Placing a variable in the tightest scope (function level) means it will be thrown away the soonest, but if you plan on calling that function a great deal of times in a row then you will be re-instantiating the variable multiple times, causing less efficiency. Define great deal as any high two or three digit number (this is a guess).

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top