SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    ASP Functions (link required)

    Anyone seen a really good article on ASP functions? What they are, how to write them, etc?

    Reason I ask is I'm just about done a documentation set for a project we recently completed and I'd rather use something ready made then write it myself (it'll probably be clearer after 5 hours of writing).
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  2. #2
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    never mind, got it... thanx

    You can write functions in ASP similar to the way you write them in VisualBASIC. It is good programming practice to use functions to modularize your code and to better provide reuse. To declare a subroutine (a function that returns no value), you simply type:


    <%@ LANGUAGE="VBSCRIPT" %>
    <%
    sub SubroutineName( Parameters to Pass In )
    'Code for Sub...
    end sub
    %>
    A function differs from a subroutine in the fact that it returns data. To declare a function, the syntax is similar:


    <%@ LANGUAGE="VBSCRIPT" %>
    <%
    function FunctionName( Parameters to Pass In )
    'Code for Function...
    end function
    %>
    Let's look at the code for a function that takes an integer value and returns the square of that value. Also included is code to call the function.


    <%@ LANGUAGE="VBSCRIPT" %>
    <%
    function Square(num)
    Square = num * num end function


    'Returns 25
    Response.Write(Square(5))

    'Should print "40 is less than 8^2"
    if 40 < Square(8) then
    Response.Write("40 is less than 8^2")
    else
    Response.Write("8^2 is less than 40")
    end if
    %>
    If you do not understand an if statement, this tutorial should help!

    To return a value from a function, you need to say the function's name = some value. That value is what is returned. In this case, we are returning num (the number passed in) times itself, or essentially num^2.

    Important! Whenever you call a function and expect it to return a value, you must use parenthesis to pass in the parameter(s). For example, we used Square(8). If you are calling a subroutine, you cannot use parenthesis. If we want to pass in value(s), we need to put a space after the sub name and then each parameter separated by a comma. Observe the example below:


    <%@ LANGUAGE="VBSCRIPT" %>
    <%
    sub PrintProfit(Revenue, Overhead, COGS, Admin)
    'Create a variable to store our profit
    Dim Profit

    Profit = CDbl(Revenue - (Overhead + COGS + Admin))

    Response.Write("$" & Profit)
    end sub


    'Call the sub with various values
    'Will output $100.0
    PrintProfit 150, 25, 10, 15

    'Will output $0.5 (a bad year!!)
    PrintProfit 200, 150, 45, 4.5
    %>
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  3. #3
    SitePoint Evangelist Umair.ms's Avatar
    Join Date
    Sep 2000
    Location
    Lahore, Pakistan
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My article about Recursive Functions, that I wrote for ASPWatch.com, but was also published at 15seconds.com.

    http://www.15seconds.com/Issue/000803.htm
    TinyPlanet.org
    Discuss and Debate World Events, Politics and Religion.
    Interact and Share your Views with People around the Globe.


Bookmarks

Posting Permissions

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