Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Variables passed to/from functions
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Variables passed to/from functions

 Post Reply Post Reply
Author
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post Topic: Variables passed to/from functions
    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.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post Posted: 06 September 2005 at 8:07pm

Lead me not into temptation... I know the short cut, follow me.
Back to Top
ub3rl337ch3ch View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 February 2005
Location: Australia
Status: Offline
Points: 341
Post Options Post Options   Thanks (0) Thanks(0)   Quote ub3rl337ch3ch Quote  Post ReplyReply Direct Link To This Post 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


Edited by ub3rl337ch3ch - 06 September 2005 at 8:20pm
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.


Edited by dpyers - 07 September 2005 at 5:08am

Lead me not into temptation... I know the short cut, follow me.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.