ASP.NET Basics (Part 6): Fully Function-al - First Date
(Page 7 of 8 )
If you've programmed in other languages before, you'll know that generally, a function can return only a single value. However, this does not hold true for functions in C#, which can return more than one value. Take a look:
<SCRIPT language=c# runat="server">
// define a function
string User(string name, string place, out string comment)
{
comment = "Care to join me for some coffee tonight?";
return "Hello, " + name + ". How strange that we both live in " + place;
}
void Page_Load()
{
string propose;
output.Text = User("FunkyChameleon", "London", out propose);
output.Text = output.Text + "
" + propose;
}
</SCRIPT>
<asp:label id=output runat="server"></asp:label>
There are two things to look for here. First, take a look at the updated "User" function.
string User
(string name, string place, out string comment) { comment = "Care to join me for some coffee tonight?"; return "Hello, " + name + ". How strange that we both live in " + place; }
I have added a new parameter named "comment" to the list of function arguments, prefixed with the keyword "out". This little addition instructs the C# compiler to allow the function to assign a value to this "comment" variable, which is then transferred to a variable specified when the function is invoked. This is clearly visible in the lower half of the script.
output
.Text = User("FunkyChameleon", "London", out propose);
Here, I have defined a variable named "propose" to pass as a parameter to the updated "User" function. Once the "User" function is invoked, the value assigned to the function variable "comment" is transferred to the "propose" variable once the function has completed executing. Note the use of the "out" keyword in the function invocation as well, to map the function's return argument to the caller's variable.
Next: Flavor of the Month >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire