ASP.NET Basics (Part 6): Fully Function-al - The Right Spirit
(Page 2 of 8 )
Let's take a simple example, which demonstrates how to define a function and call it from different places within a C# script:
<SCRIPT language=c# runat="server">
// define a function
void guaranteedPickMeUp()
{
Response.Write("Long Island Iced Tea");
}
void Page_Load()
{
// the first question...
Response.Write("What's the only thing that cheers me up when I'm down?
");
// call the function
guaranteedPickMeUp();
// ... followed by another
Response.Write("
What contains a virtual menagerie of spirits, guaranteed to leave your head banging in the morning?
");
// call the function
guaranteedPickMeUp();
}
</SCRIPT>
View it in your browser - you should see something like this:
Let's take this line by line. The first thing I've done in my C# script is define a new function with the "function" keyword; this keyword is followed by the name of the function and the data type of the return value of the function. I have used "void" here because our function, in its current state does not return any value (more on this at a later date, though). In the example above, the function has been named "guaranteedPickMeUp".
All the program code attached to that function is then placed within a pair of curly braces - this program code could contain loops, conditional statements, calls to other functions, or calls to other C# functions. Function names are case-sensitive in C#, so be careful when invoking them.
Here's the typical format for a function:
return_data_type function_name
( optional function arguments ) { statement 1; statement 2; statement 3; . . . statement n; }
Next: Turning Up the Heat >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire