Print Page | Close Window

Functions within 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=15624
Printed Date: 30 March 2026 at 3:25am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Functions within Functions
Posted By: ub3rl337ch3ch
Subject: Functions within Functions
Date Posted: 27 June 2005 at 10:04pm
I have the following two functions, the checknull one being called repeatedly in the blah() one (obviously the entire blah function isn't there) . If FarmName has length 0, the output from it all is TrueFalseTrue.
 
Basically, it sets valid as False, and writes it as such within the function, but then when it leaves the chenull function to go back to the main part of the blah function, valid is not longer False and is treated as though it had never changed.
 
Is this because I am using one function within another? Any other suggestions which might shed some light on this? thx
 
 
 
function checknull(theString,validvar)
theString = Trim(theString) validvar = "True"
if Len(theString) = 0 then
valid = "False"
validvar = "False"
end if
Response.Write valid
end function
 
 
function blah()
valid = "True"
call checknull(FarmName,vFarmName)
Response.Write valid
end function



Replies:
Posted By: ub3rl337ch3ch
Date Posted: 27 June 2005 at 10:13pm
I suppose I should add that I have never had a problem with the valid variable's value carrying through from the checknull function, but I have never tried to use it as a part of another function before.


Posted By: dpyers
Date Posted: 28 June 2005 at 5:41am
Dim valid before the functions. Your problem is that the scope of the variable is restricted to within the function.  Also use By Val for the parameters. Valid may also be a reserved word.

Technically what you have coded are sub's, not functions. A function should only return a value of true/false.

I would have done it something like...

Dim strTheString
Dim strValid
strTheString = "xxxxx"

Function fncCheckNull (By Val strTheString)
'True if strTheString contains non-blank characters
   If Len(Trim(strTheString) > 0 Then
      fncCheckNull = True
   Else
      fncCheckNull = False
   End If
End Function

Sub subBlah()
   If fncCheckNull(strTheString) Then
      strValid = "True"
   Else
      strValid = "False"
   End If
   Response.Write strValid
End Sub

  


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

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


Posted By: ub3rl337ch3ch
Date Posted: 28 June 2005 at 6:34pm
what does By Val do? I've never encountered that before.


Posted By: dpyers
Date Posted: 28 June 2005 at 6:44pm
Actually, I think I should have said By Ref instead of By Val.

By Val means the value within a string/whatever is passed to the function in a separate memory space. Changing the value in a function does not affect the original string.
By Ref means to pass a reference to the location of the string. Changing the value in a function changes the original source.


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

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


Posted By: ub3rl337ch3ch
Date Posted: 28 June 2005 at 6:47pm
great, thanks.
 
Do you have any idea why it would be working with only the single function, but not that same function within another? is it something asp isnt capable of? or should it be possible to have functions/subs within other functions/subs?


Posted By: dpyers
Date Posted: 28 June 2005 at 7:19pm
I think if you Dim the variable before calling either function and use By Ref, changing the contents wherever will stick.

Here's an article from my bookmarks That explainms things better than me.
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: 28 June 2005 at 7:23pm
cheers



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