BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Functional Methods

The forty-fifth part of the C# Fundamentals tutorial expands upon the creation of methods described in the previous instalment. In this article, the creation of methods that return a value and that can accept parameters is considered.

Functional Methods

In the previous article simple C# methods were created in order to centralise tasks, increasing readability and maintainability of code. These methods worked in isolation as there was no facility to pass information to the function and no value returned on completion. Although useful, most methods that you create will require parameters, return values or both.

Creating a Method that Returns a Value

Previously we created a method that obtained the current date and time and outputted it to the console in a desired format. In this article, we will create a method that returns the date and time rather than outputting it. Methods can be created that use any value type or reference type for the return value. In this case, the formatted date and time will be returned as a string.

To declare a method with a return value the return type is indicated first, followed by the name of the method and a pair of parentheses (). The code for the method is added within a code block. The code to declare our empty example method is therefore as follows:

string GetFormattedDate()
{
}

To return a value to the calling procedure, the return command is used, followed by the value or object that is to be passed back, as follows:

string GetFormattedDate()
{
    DateTime theDate = DateTime.Now;
    return theDate.ToString("dd/MM/yyyy");
}

With the method created, it can be called in the same manner as any other method. If you are using a console application, the default class will be named Program and will contain a Main method as well as GetFormattedDate. To use the new method, first create a new Program object then call its GetFormattedDate function, assigning the result to a variable. This can be controlled within the Main method making the final code as follows:

static void Main(string[] args)
{
    Program p = new Program();
    string outputDate = p.GetFormattedDate();

    Console.WriteLine(outputDate);              // Outputs "26/07/2007"
}

string GetFormattedDate()
{
    DateTime theDate = DateTime.Now;
    return theDate.ToString("dd/MM/yyyy");
}

Adding Parameters

So far, the methods created in the tutorial only perform a single task and their operation cannot be configured. By adding parameters, information can be passed from the calling function to the method to control the processing.

Any number of parameters may be added to a method by declaring each parameter within the method's parentheses, separated by commas. Each parameter must have a data type that is declared using the same syntax as when creating an uninitialised variable.

For this example, we will create a method that accepts two parameters that define the width and height of a rectangle. The values will be multiplied to obtain the rectangle's area and the resultant value returned. Firstly, the method is defined with the return value and the two parameters specified. Note that the parameters are defined in lower camel case. This means that the initial letter of each word in a name is capitalised, except for the first word, which is lower case. This is a useful convention for variable and parameter naming.

int GetArea(int rectHeight, int rectWidth)
{
    return rectHeight * rectWidth;
}

You can now call the method with the parameter values, or arguments, specified:

static void Main(string[] args)
{
    Program p = new Program();
    int area = p.GetArea(10, 5);

    Console.WriteLine(area);    // Outputs "50"
}

int GetArea(int rectHeight, int rectWidth)
{
    return rectHeight * rectWidth;
}

Static Methods

In the example above, a new object of the class Program was created so that the method could be executed. You may have noticed that the Main method was declared differently to the OutputFormattedDate method. Specifically, it has the static prefix. This declares the method as a static method, meaning that no object needs to be created before the method is called. By prefixing the new method with the same static keyword, we can remove the requirement to create a Program object.

static void Main(string[] args)
{
    int area = GetArea(10, 5);

    Console.WriteLine(area);    // Outputs "50"
}

static int GetArea(int rectHeight, int rectWidth)
{
    return rectHeight * rectWidth;
}
26 July 2007