The main purpose of using a function on a web page is to perform an assignment. In some cases, you may want that function to produce a result. The function is said to return a value. To indicate that a function must return a value, precede its name with the type of value that it would return. Here is an example: <script runat="server"> int GetNumber() { } </script> In the body of the function, you can perform any type of assignment that is necessary. The most important rule is that, before the function is closed, that it, before the closing curly bracket, the function must have returned a value of the appropriate type. To return a value from a function, type the return keyword, followed by the value being returned, followed by a semi-colon. Here is an example: <script runat="server"> int GetNumber() { return 15; } </script> You can also declare a local variable, process it, and return it from a function. Here is an example: <script runat="server"> int GetNumber() { int number; number = 15; return number; } </script>
Some functions will need to receive a value in order to perform their assignment; some others will not. Some functions will have many needs and some others will not. A function’s need is called a parameter. A parameter is the type of the variable that the function would need to perform its assignment. The most important aspect of a parameter is its data type. This data type is required when the function is defined.
The type of value that a function needs is called a parameter. The actual value that would be processed by the function is called an argument. Here is an example of a function that takes an argument: <script runat="server"> void Multiply(double Value) { } </script> When a function receives an argument, it can ignore that argument. To call a function that takes an argument, specify the name of the function and its list of arguments (if any) inside of parentheses. You can pass the constant value of an argument. When calling the function, if the value is stored in a variable, you can pass the name of the variable in place of the argument. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> void Multiply(double Value) { } </script> <title>Exercise</title> </head> <body> <% double number; number = 19.95; Multiply(number); %> </body> </html> When defining a function that takes a parameter, in the body of the function, you can use the argument appropriately as you see fit. One of the minimum operations you can perform is to involve the argument in an expression. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> void Multiply(double Value) { double Weekly; Weekly = Value * 36.50; Response.Write("Hourly Salary: $" + Value); Response.Write("<br />"); Response.Write("Weekly Salary: $" + Weekly); } </script> <title>Exercise</title> </head> <body> <% Multiply(19.95); %> </body> </html> This would produce: Hourly Salary: $25.75 Weekly Salary: $939.875 Press any key to continue . . . You can also define a function that returns the value of the argument. Remember that, depending on the goals you set for your function, you can use as many parameters as you want. Here is example: <script runat="server"> void CalculateRateAmount(double price, double rate) { } </script> As seen in previous sections, the return keyword is used to return an appropriate value from a non-void function. In fact, the item on the right side of the return keyword could be a constant value or a complete expression. Here is example: <script runat="server"> double CalculateRateAmount(double price, double rate) { return price * rate / 100; } </script> To call a function that takes more than one parameter, provide a value for each parameter in the parentheses of the function, in the order the parameters are listed in the function.
The name of a function, its return type, and its list of arguments, if any, constitute the function's signature. This signature gives complete information regarding a function. This information is used to create a table (called a virtual table) of all functions used in a particular file. In this table, each function is assigned a unique identification using the function's name, its return type, and its argument(s) (this is referred to as name mangling). The ability for two functions to have the exact same name but differ either by their type(s) of argument(s) or by their number of argument(s) is called function overloading. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> // Area of a square float Area(float Side) { return (Side * Side); } // Area of a rectangle float Area(float Length, float Width) { return (Length * Width); } </script> <title>Exercise</title> </head> <body> <% float s, l, w; s = 15.25f; l = 28.12f; w = 10.35f; Response.Write("The area of the square is " + Area(s)); Response.Write("<br />The area of the rectangle is " + Area(l, w)); %> </body> </html> This would produce: |
|
|||||||||||
|