Lesson 3.3 Functions

A function is simply a special kind of method. It tells you something, there’s a result from it that it gives you back. It returns a value.

A real life example is the way you might ask someone for directions somewhere. You give them the destination you’re trying to get to and they, hopefully, give you how to get there.

As always, the best way to show a principle is to show it in action.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Methods4
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 1; x <= 10; ++x)
            {
                double circleArea = GetCircleArea(x);

                System.Console.Write("A circle with radius of ");
                System.Console.Write(x);
                System.Console.Write("cm has an area of ");
                System.Console.Write(circleArea);
                System.Console.WriteLine(" cm²");
            }

            System.Console.ReadKey(true);
        }

        static double GetCircleArea(int radius)
        {
            double area = Math.PI * radius * radius;
            return area;
        }
    }
}

This code above shows the area of 10 circles. It’s quite simple and hinges on the old maths formula that the area of a circle is π (3.141592) multiplied by the radius squared.

Radii results

Firstly, there is a loop going from 1 to 10 for the radius of the circle.

    for (int x = 1; x <= 10; ++x)
    {
        double circleArea = GetCircleArea(x);

        System.Console.Write("A circle with radius of ");
        System.Console.Write(x);
        System.Console.Write("cm has an area of ");
        System.Console.Write(circleArea);
        System.Console.WriteLine(" cm²");
    }

The first thing I do within the loop is create a decimal variable. This variable is where I am dumping the area of the circle. This is getting the results of a call to a function I have created called GetCircleArea, snappy I know.

    static double GetCircleArea(int radius)
    {
        double area = Math.PI * radius * radius;
        return area;
    }

This looks quite similar to a normal method, and it is, but it has two main differences. The first difference is the header of the method.

  static double GetCircleArea(int radius)

It doesn’t use void anymore, it uses the word double instead. This tells C# the type of value that this function is going to give. The area of a circle will be given as a double. A handy way to think about it is that a method is simply a function that doesn’t return anything. Hence the void keyword on methods, there is nothing returned.

This function takes the radius of the circle as a parameter. It is handy to note that functions, just like methods, don’t necessarily need parameters. It depends on the circumstances you’re using it for.

The second difference is the presence of a return statement. This is very important. You cannot have a function without a return statement. It will not run.

  return area;

This is the point in the function that the result of the work you have done is passed back. In this case I have worked out the area of the circle and placed it into a variable called area.

  double area = Math.PI * radius * radius;

Note that in C#, as with most languages, a star symbol, *, is a multiplication symbol. It then returns that area value.


Supporting files

File Description Open with
Methods 4 Code demonstrating the radius calculating function Visual C# 2008