Print Page | Close Window

Variables passed to/from functions

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=16449
Printed Date: 29 March 2026 at 11:56pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Variables passed to/from functions
Posted By: ub3rl337ch3ch
Subject: Variables passed to/from functions
Date Posted: 05 September 2005 at 9:30pm

I'm having trouble getting variables to follow through from a function in which their value is altered. As previously suggested by people here I declared said variables first so they are classified as variables on a program-wide scope rather than just within the function.

I have also attempted to call the function specifically with the var names eg: call functionname(var1,var2,var3,var4,var5,var6)

the problem is that in a later part of the program i need to call var1 through 6 within another function, where they are written to the database. When they don't get passed from function1, they default to the null value from when they were dim'ed, and i get a whole bunch of blank values in the database.

Is there anything that i'm forgetting to do to get the values to pass properly? or am i just going to have to hard-code the functions in the places they're required. For obvious reasons i'd like to avoid this at any cost.



Replies:
Posted By: dpyers
Date Posted: 06 September 2005 at 8:07pm
Good tut here - http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=33 - http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=33




-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: ub3rl337ch3ch
Date Posted: 06 September 2005 at 8:19pm
i discovered that the reason it wasn't working was that i was attempting to use the values from function1 in function2, like:
 
 
dim var1, var2
 
function function1()
var2 = "bill"
end function
 
function function2()
if var1 = "bob" then
...
end if
if var2 = "bill" then
...
end if
end function
 
var1 = "bob"
call function1()
call function2()
response.write var1
response.write var2
 
for some reason while var1 can be read by function1 and function2 and the r.w in the main code, var2 cannot be read by function2, only the main code. even specifying the parameters, and specifying byref doesn't have any impact.
i discovered that if i nested function2 within function1:
 
function function1()
var2 = "bill"
call function2()
end function
 
there is no propblem...
 
 
then again the reason i had so much trouble may be that i have about jack all theoretical knowledge of asp LOL


Posted By: Gullanian
Date Posted: 07 September 2005 at 12:40am
Reading up on basic programming architecture will help you with this.  You can pass variables through into functions, which would create a better system because variables would have a local rather than global scope.


Posted By: dpyers
Date Posted: 07 September 2005 at 5:02am
Check out the differences between functions and subroutines as well. A function is really just supposed to return a referenceable value and a sub routine fills multiple variables.

So if you had a function like...
Function fncX (A, B)
    If A = B then
        fncA = true
       Else
       fncA = false
End Function


Then you check it inline by

If fncX("sss", "sss") then ...


or you do something like
Dim A
Dim B
Function fncGetIt()
    fncGetIt = A + B
End Function


And in your code you'd do something like
~code to manipulate A and B ~
Response.Write fncGetI
t

Most asp commands are just functions - e.g date, instr, trim, etc. Functions allow you to write your own reuseable commands whereas subroutines are reuseable blocks of application code.  It's admittedly a fine distinction but a rule of thumb is if you can wrap the logic and use it like a command, it should be a function. If not, then it should be a subroutine.


-------------

Lead me not into temptation... I know the short cut, follow me.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net