Home

Introduction to Functions

  

Scripts

 

Introduction

There are various types of places where you will write your code. As seen above, you can create delimiting sections where you can write your code. Some other types of code will require that you create your code in the head section. To write ASP.NET code in the head section, you must create a script.

 

Creating a Script

To start creating a script in the head section, starts a <script> tag and close with an end </script> tag. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script>

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

The <script> tag uses various attributes. To start, you must specify that the script will run on the server. To do this, add an attribute named runat to the tag and assign the server string to it. This can be done as follows:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

After doing this, you can create your code between the starting <script> and the end </script> tags.

When writing your code, because you have determined that the page will use the C# language, you can simply write your code as you see fit. Still, you have the option of choosing among various languages. The available languages are C#, VB, VBSscript, JavaScript, JScript, or ECMAScript. To let you specify the language, the <script> tag is equipped with an attribute named language. To specify the language, assign it to the language attribute. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script language="C#" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

If you don't specify the language, C# is assumed. Besides the language, you can specify how the code of your script will be formatted or considered. To support this, the <script> tag is equipped with the type attribute. To specify it, assign:

  • text/C# to the type attribute if you had, or will, specify the language as C#
  • text/VB to the type attribute if you had, or will, specify the language as VB
  • text/vbsscript to the type attribute if you had, or will, specify the language as VBScript
  • text/javascript to the type attribute if you had, or will, specify the language as JavaScript
  • text/jscript to the type attribute if you had, or will, specify the language as JScript
  • text/ecmascript to the type attribute if you had, or will, specify the language as ECMAScript

Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script language="C#" type="text/C#" runat="server">

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Remember that only the runat attribute is required. The others are optional. After specifying the values of the desired attributes, you can create your code.

Introduction to Functions

 

Definition

You can divide your code is smaller or relatively smaller sections of code so that each section is meant to perform a specify task. Other sections of the program can then refer to that section for whatever it has to provide. Such a section of code is called a function.

Fundamentals of Creating a Function

To create a function, there are some rules you must follow. The fundamental formula is:

ReturnType FunctionName() { }

The first rule is that the function must be created outside of any other function.

The ReturnType is the kind of value that the function will produce. Some functions don't produce a (specific) result. In this case, the ReturnType can be substituted with the void keyword.

Like a variable, a function must have a name. The name follows the rules we have applied to variables so far. The name of a function must be followed by parentheses.

The parentheses of a function must be followed by an opening curly bracket "{" and a closing curly bracket "}". The section inside of the curly brackets is referred to as the body of the function. In this body, you perform the assignment of the function. For example, a function can be used to simply display a sentence. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
void Welcome()
{
    Response.Write("Welcome to our web site.");
}
</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

Writing a function like this, with its name and body, is referred to as defining or implementing it. In the same way, you can define or implement as many functions as you judge necessary.

Calling a Function

After creating a function, you can use it either inside of itself or from another function. Using a function is referred to as calling it.

To call a function, type its name, followed by parentheses. If the function is called as a statement, then it must be terminated with a semi-colon. Here is an example:

<%@ Page Language="C#" %>

<html>
<head>

<script runat="server">
void Welcome()
{
    Response.Write("Welcome to our web site.");
}
</script>

<title>Exercise</title>

</head>
<body>

<%
    Welcome();
%>

</body>
</html>

This would produce:

Function

 
 
 
     

Returning a Value

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>

Functions Parameters

 

Introduction

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.

Passing an Argument

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.

Function Overloading

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:

Function Overloading

 
 
   
 

Previous Copyright © 2009-2016, FunctionX, Inc. Next