Home

Introduction to Functions

Fundamentals of Functions

Introduction

You can divide your code is smaller or relatively smaller sections of code so that each section is meant to perform a specific 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:

return-type function-name() { }

The return-type is the type of value the function will produce. Some functions don't produce a (specific) result. In this case, the return-type must 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. A function is usually made to perform an action. Therefore, its name should convey an action. 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#" %>

<!DOCTYPE html>

<html>
<head>

<script runat="server">
void Show()
{
    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.

Calling a 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#" %>

<!DOCTYPE html>

<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:

Calling a Function

 
 
 
     

Returning a Value

After performing its action, a function may produce a value. This is also said that the function returns a value. If a function is meant to poduce a value, you must indicate the type of value. To indicate that a function must return a value, precede its name with the type of value 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 closing curly bracket, the function must have produced, or 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 its function. Here is an example:

<script runat="server">
int GetNumber()
{
    int number;

    number = 15;

    return number;
}
</script>

Functions Parameters

Introduction

Some functions will need 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 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#" %>

<!DOCTYPE html>

<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 types of operations you can perform is to involve the argument in an expression. Here is an example:

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

<!DOCTYPE html>

<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:

Passing an Argument

You can also define a function that returns the value of the argument.

A Function with Many Parameters

Depending on the goals you set for your function, you can use as many parameters as you want. The parameters are specified in the parentheses of the function. Each parameter must have its own data types. The parameters are separated by commas. Here is an 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, is referred to as the function's signature. This signature gives some information about the 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).

Two functions can have the exact same name but differ either by their type(s) of argument(s) or by their number of argument(s). This feature is called function overloading. Here is an example:

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

<!DOCTYPE html>

<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, C# Key Next