Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Call a Sub inside a function

    Is it possible to call a sub inside a function?

    example

    Function name()
    blah
    Call Thesub()
    End Function

    Thesub()
    blah
    End Sub


  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you tried it? If you're just calling it it works, the only limitation is you can't contain one inside the other...

    P.S. I tested it just as easily as you posted:

    Code:
    <%
    Sub blah()
    	Response.Write("BLAH")
    End Sub
    
    Function whatever()
    	Call blah()
    End Function
    
    whatever()
    %>
    Last edited by whammy; 11-26-2003 at 02:00 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah but it didnt work! I tried below!

    Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName)
    Dim GUID, IsOk, SQL, RS
    IsOk = False

    Dim Counter
    Counter = 0

    'loop round generating new GUIDs until we've found one that hasn't been used yet!
    While Not IsOk
    Counter = Counter + 1

    GUID = MakeGUID()

    SQL = "SELECT " & GUIDFieldName & " FROM " & TableName & " WHERE " & GUIDFieldName & " = '" & GUID & "'"
    'Response.Write SQL & "<BR />"
    'Response.Flush

    Set RS = ExecuteQuery(Connection, SQL, False, True)

    If RS.EOF Then
    'No matching GUID found, so it really *is* unique
    IsOk = True

    sqla = "insert into CouponCode (StartDate, ExpireDate, ToField, Referal, FromField, Catagory, CCode, CCDValue, Used, Ip) values ('" & date & "', '" & ((date)+365) & "', '" & ToField & "', '" & Referal & "', '" & FromField & "', 'All', '" & GUID & "', '" & CCDValue & "', '-1', '" & Ip & "');"

    Conn.Execute(sqla)

    Call Email()
    End If

    Kill RS
    Wend

    'Response.Write "GenerateGUID took " & Counter & " attempts to generate a new GUID.<BR>"
    GenerateGUID = GUID
    End Function
    <%Sub Email() '''''''''''''''''''''''''''''''''''''''%>
    <%
    Response.Write GUID
    %>
    <%End Sub '''''''''''''''''''''''''''''''''''''''''''%>
    but it doesnt work?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's because you dimension the variable inside of the function, limiting its scope to "local", i.e. within the function. Once you exit the function (even with a call to a sub), the variant is destroyed.

    If you want the variable to be global you need to dimension it outside of the function, or pass the variable to said sub. Does that make sense?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not really? I do realize that the sub does work I added a Response.Write "Blah"

    and that came up! So its the GUID that is not coming up really odd cause if I put the
    Response.Write GUID is where I put the Call Email() it works?
    Last edited by Crash1hd; 11-26-2003 at 02:06 AM.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It's a basic programming tenet; if you declare a variable within a function (or subroutine or class), then it only exists as long as that function is around. Once you go outside of the function, that variable (or variant, in this case) no longer exists!

    So in order to fix the problem you're having, the easiest solution is to remove the Dim statement from within your function and put it outside.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok now that I understand!

    and it works great thankyou

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts


    It might benefit you to do a google search on "variable scope" or somesuch; it applies to all programming languages as far as I know. I'm glad it's working now.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #9
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    Crash1hd, when helping you over MSN the other night you said that you had read through the "VBScript User Guide" in the Windows Scripting help file that I posted the link to the other day... it seems not... see the attached screenshot of the fourth topic of the first section...

    Attached Thumbnails Attached Thumbnails Call a Sub inside a function-variablescope.gif  
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You know I read that too! When whammy had mentioned it I realized how dumb my question was lol, I guess I always look for the hard way when its right under my face!

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is a pretty important concept when it comes to programming. Like I said, this applies to every language I know of (which isn't many, lol)... but programming is pretty much the same except for syntax from language to language, barring features unique to the langauge and syntax.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •