Mr. Martin's programming school

Functions

« Program flow Classes »
So, what is this magic thing called functions, and why should you have them? Actually, you have been using them all along. If I was to be a bit technical, I could point out that all exercises so far, being console programs, are implemented as a function. The function main(). You can start them from a command prompt by typing its name when the program is in your path or your current folder. What happens is that you tell the operating system to run the function main() in the binary block of code found in your executable. The linker has put in a table of function names and where to find them in the executable, and the command prompt is designed to look for a function called main().

To be less techical, I could just say that a function is a bunch of code that has been marked with a name in order to make it possible to find.

Just think about it. You have alreade used functions like Int32.Parse(), Console.WriteLine() etcetera. Those functions comes with the API, and they are baked into the classes Int32 and Console respectively. We will study classes in the next chapter.

Imagine that you write a program where you need to use the bubble-sort algorithm more than once. First to sort one list, and then to sort another list, etcetera. Would you really want to write the same code in more than one place? Of course not! You would rather make a function and put a slightly modified version of your code into it.

Function aguments

Modified? Why? Well, isn't that rather obvous? If it should sort one list or another one it needs to know which list to sort.

That is where the arguments come in. You have already used arguments. The arguments are the values you put between the parantheses, like this:

    Console.WriteLine("Hello world!");

That's right. The text is an argument for the code in WriteLine() to use.

Maybe you noticed that the main() function actually has a list of strings as an argument. What could they be? Try the following code:

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

namespace Arguments
{
    class Program
    {
        static void Main(string[] args)
        {
            for (Int32 i = 0; i < args.count(); i++)
            {
                Console.WriteLine(args[i]);
            }
        }
    }
}

Create this program and call it 'Arguments', and build it. Then open a command prompt and navigate to your project, and down to the /bin/Debug, or /bin/Release if you built for release. You can now start the program by typing the word 'Arguments' (without the quotes). If you add more text, separated by spaces, you will see that the program actually echoes those strings.



What if we need to debug the program? In that case we must be able to add arguments from Visual Studio, and there is a way to do just that. Try the menu Debug -> Arguments Properties...



Here you can insert any arguments:



If you just run the program without stepping through the code it would be convenient to add our ReadKey():

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

namespace Arguments
{
    class Program
    {
        static void Main(string[] args)
        {
            for (Int32 i = 0; i < args.count(); i++)
            {
                Console.WriteLine(args[i]);
            }
            Console.ReadKey();
        }
    }
}

Now you can see that it works fine when you press e.g. F5:



You now have an easy way to test your program with arguments.

Exercise:

If you like, I have an extra exercise for you here:

  1. Create a console program called 'SortNumbers'.
  2. The program should accept any number of integers as arguments.
  3. Create a list of integers the same size as the list of arguments.
  4. Use Int32.Parse() to populate the list from the arguments.
  5. Insert a bubble-sort algorithm to sort the list of integers.
  6. Print the ordered list of integers, one per line.
  7. Build the program and run from a command prompt.
This is what a test-run could look like:





You now know how to handle arguments, but let's concentrate on creating functions you call yourself.

Let's start with a simple one:

    static Int32 Add(Int32 a, Int32 b)
    {
        return a + b;
    }

First, there is a keyword 'static'. I cannot explain that keyword without getting into classes and objects, which is not the current agenda. Let us discuss that later when we take a look at classes.

Int32 is the return type. It tells the compiler that the function is supposed to return an integer. It must return an integer, or maybe null, if that is called for. The keyword 'null' means 'nothing'. In some situations you migth have use for returning null when e.g.something went wrong.

Then we have 'Add', which is the name of the function. It is a name just like we name our variables. We simpy need a name in order to be able to refer to the function.

The pharenthesis holds a list of zero or more arguments. In this case the function accepts two Int32 values. Nothing more, nothing less. It does not accept null, but we do have a syntax for that, and we will discuss that later.

The function body is enclosed within a pair of curly bracesers.

Let us do a short program that calls the function:

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

namespace FunctionAdd
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 c = Add(29, 13);
            Console.WriteLine(c.ToString());
            Console.ReadKey();
        }

        static Int32 Add(Int32 a, Int32 b)
        {
            return a + b;
        }
    }
}

When we run it, it will of course print the value 42.

In this case I supplied two constants for arguments. I could as well have supplied variables, or one variable and one constant. It is all the same to the function.