ASP.NET
  Home arrow ASP.NET arrow Page 5 - ASP.NET Basics (Part 6): Fully Function-al
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Basics (Part 6): Fully Function-al
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2003-11-11

    Table of Contents:
  • ASP.NET Basics (Part 6): Fully Function-al
  • The Right Spirit
  • Turning Up the Heat
  • Sweet Tooth
  • Passing the Buck
  • Going Nowhere
  • First Date
  • Flavor of the Month

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Basics (Part 6): Fully Function-al - Passing the Buck


    (Page 5 of 8 )

    Now, if you've been paying attention, you've seen how functions can help you segregate blocks of code, and use the same piece of code over and over again, thereby eliminating unnecessary duplication. But, this is just the tip of the iceberg.

    The functions you've seen thus far are largely static, in that the variables they use are already defined. It's also possible to pass variables to a function from the main program; these variables are called "arguments", and they add a whole new level of power and flexibility to your code.

    To illustrate this, let's go back a couple of pages and revisit the tempConv() function.

     double tempConv() { double celsius 35double fahrenheitfahrenheit = (celsius 1.8) + 32; return fahrenheit; } 



    As is, this function will always calculate the Fahrenheit equivalent of 35 degrees Celsius, no matter how many times you run it or how many roses you buy it. However, it would be so much more useful if it could be modified to accept *any* value, and return the Fahrenheit equivalent of that value to the calling program.

    This is where arguments come in - they allow you to define placeholders, if you will, for certain variables; these variables are provided at run-time by the main program.

    Let's now modify the tempConv() example to accept an argument.


    <SCRIPT language=c# runat="server">

    // define a function
    double tempConv(double temperature
    {
            
    double fahrenheit;
            
            
    fahrenheit = (temperature 1.8) + 32;
            
            return 
    fahrenheit;
    }

    void Page_Load()
    {   
        
    string result;
        
        
    double alpha 45;
        
        
    result tempConv(alpha).ToString(); 
        
    output.Text alpha.ToString() + " Celsius is " result " Fahrenheit.";


    </SCRIPT>
     <asp:label id=output runat="server"></asp:label>



    And now, when the tempConv() function is called with an argument, the argument is assigned to the placeholder variable "temperature" within the function, and then acted upon by the code within the function definition.

    Note that it's mandatory to specify the data type of the arguments being passed to the function.

    It's also possible to pass more than one argument to a function - as the following example demonstrates.


    <SCRIPT language=c# runat="server">

    // define a function
    string addIt(string item1string item2string item3
    {
        return 
    item1 item2 item3;
    }

    void Page_Load()
    {   
        
    output.Text addIt("hello""-""john");


    </SCRIPT>
     <asp:label id=output runat="server"></asp:label>



    And its output.

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...
    - Completing an ASP.NET AJAX Server-Centric Ba...
    - Information Management for an ASP.NET AJAX S...
    - Comment and Order Management for an ASP.NET ...
    - Back-end Management Tasks for an ASP.NET AJA...
    - User Information Management for an ASP.NET A...
    - Adding Comments and Search to an ASP.NET AJA...
    - Order-Related Modules for an ASP.NET AJAX Se...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway