ASP.NET Basics (Part 6): Fully Function-al - Turning Up the Heat
(Page 3 of 8 )
By default, when a function is invoked, it always generates a "return value". This return value can either be a special return type called "void", or a value explicitly returned via the "return" statement. I have already demonstrated the use of "void" for my "guaranteedPickMeUp" function, which does not return any value. But how about a function that does return a value?
Here's an example of how a return value works.
<SCRIPT language=c# runat="server">
// define a function
double tempConv()
{
double celsius = 35;
double fahrenheit;
fahrenheit = (celsius * 1.8) + 32;
return fahrenheit;
}
void Page_Load()
{
string result;
result = tempConv().ToString();
output.Text = "35 Celsius is " + result + " Fahrenheit.";
}
</SCRIPT>
<?xml:namespace prefix = asp /><asp:label id=output runat="server"></asp:label>
The output is as follows:
By default, when a function is invoked, it always generates a "return value". This return value can either be a special return type called "void", or a value explicitly returned via the "return" statement. I have already demonstrated the use of "void" for my "guaranteedPickMeUp" function, which does not return any value. But how about a function that does return a value?
Here's an example of how a return value works.
<SCRIPT language=c# runat="server">
// define a function
double tempConv()
{
double celsius = 35;
double fahrenheit;
fahrenheit = (celsius * 1.8) + 32;
return fahrenheit;
}
void Page_Load()
{
string result;
result = tempConv().ToString();
output.Text = "35 Celsius is " + result + " Fahrenheit.";
}
</SCRIPT>
<asp:label id=output runat="server"></asp:label>
The output is as follows:
In this case, the value of the last expression evaluated within the function is assigned to the variable "result" when the function is invoked from within the program. Note that the return value must be explicitly specified within the function definition via the "return" statement, and the data type of this return value must be specified in the first line of the function definition, before the name; failure to do either of these will cause the C# compiler to barf all over your screen.
To illustrate this, consider the output if the "return" statement is eliminated from the function definition above.
Next: Sweet Tooth >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire