What are sub routines, procedures and functions?

Sub routines is the general term for functions and procedures. Below you will find out why programmers create sub routines. What a procedure is and how it differs from a function.

What are sub routines and why bother to make them?

text

KEYWORDS on this page...

  • Sub-routines
  • procedures
  • functions
  • void
  • call
  • libraries
  • parameter
  • variable
  • argument
  • variable
  • return

You can just write all of your code into one file, build it and run it. You will have a program, so that's alright isn't it? Well no it isn't alright because:

  1. Most programmers write programs in a team and therefore need others to understand their code. They might need to find a part of your program without reading through all of your code.
  2. Most programmers contain code which carries out a specific subroutine, so it would make sense to group that code together. That code is then often reused.

So we organise code into sub routines. Sub routines group code together to perform one particular task.

How do I create a sub routine?

Let's stat with a simple procedure. Later we will build this into a more useful procedure. Then we will make a couple of changes convert the procedure into a function.

Step 1: Write your code as normal and test that it runs as intended.

If you think your code is likely to be used again, create it in your main code first. Does it build (no syntax errors). Does it work as intended (no logical errors)? Here is a simple example which will just show a message.

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Well hello there");
}

Step 2: Create the function.

You can make functions anywhere in your code. It is conventional (normal) to create them at the top just after the where it says public partial class and any variables.

To create a function you just need to type the function use the code shown below. It can be written in many different ways but for now type the two keywords: private and void followed by the name of your function name. Here the function is called DisplayGreeting.

Function names name should be a verb which describes the purpose of the function. Finish with two round brackets. The code which will go inside the function is grouped together with curly braces.

text

  • The funtion here is called DisplayGreeting.
  • Type the word private but don't worry about why for now1. Void before the name means that nothing is being passed out of the function2.
  • DisplayGreetingis followed by round brackets. In case we need to pass anything in to the function2
  • Curly braces are used to group together the function code.
1

covered in A Level Computer Science.

2

more on this later on this web page.

Step 3: Cut and paste your code into the function.

Now just cut and paste your code into the function between the curly braces.

private void DisplayGreeting()
{
    MessageBox.Show("Well hello there");
}

That's it... you have just created a function!

It doesn't do anything! How do I actually call (use) the function?

Your function is there ready to be used, but it hasn't been used yet. To call (use) the function just type its name followed round brackets as shown here.

private void button1_Click(object sender, EventArgs e)
{
    DisplayGreeting();
}

That's it your done. You have created and called your first function. However, every time you call the function, it will execute the lines of code. In this example, it will display the greeting "Well hello there."


What if I want to display a different greeting?

Step 1: Create a parameter variable

To pass data to a function you need a parameter variable which will be inserted into the round brackets. Type this as when you would when creating a variable: data type then a name. Below a a string variable is being passed into the function.

private void DisplayGreeting(String message)

Step 2: Next use your parameter variable.

In the example below the hard coded (which can't be changed) greeting has been replaced by the variable message. So whatever value is passed in will be used instead of "Well hello there." This will break the code where you call your function. Don't worry... we are about to change that too.

private void DisplayGreeting(String message)
{
    MessageBox.Show(message);
}

Step 3: Edit your function call to pass in the value you wish to use.

Change your function call by typing between the round braces the you want to pass into the function. A data value passed in is called an argument variable. In the example below, the value "hello" will be passed into the function shown above. It will be stored in the parameter variable message and used inside the function to display a message.

private void button1_Click(object sender, EventArgs e)
{
    DisplayGreeting("hello");
}

Now we have a versatile function which we can use several times with different values passed in (argument variables). The code below shows three procedure calls within three different buttons.

private void button1_Click(object sender, EventArgs e)
{
    DisplayGreeting("hello");
}

private void button2_Click(object sender, EventArgs e)
{
    DisplayGreeting("howdy");
}

private void button3_Clock(object sender, EventArgs e)
{
    DisplayFreeting("hola");
}

Each call passes a different value as the argument variable.

So that when button1 is clicked "hello" will be displayed.

But when button2 is clicked "howdy" will be displayed.

When button3 is clicked "hola" will be displayed.

How do I pass more data into my procedure?

You often need to pass in multiple parameter variables. Do this by separating each with a comma.

private void DisplayGreeting(String message, int Age)
{
    MessageBox.Show(message + "You are looking great for a " + Age.ToString() + " year old!");
}

How do I pass data out of my procedure? Convert it into a function.

Programmers usually just call all sub routines functions. But for your GCSE you need to know that functions are only functions if they pass data out. If they don't (i.e. they start with the word void) they are actually a procedure.

private int Add(int Firstval, int Secondval)
{
    int result = Firstval + Secondval;
    return result;
}
  • The function here passes out an integer value. We know this because the keyword void has been replaced with int.
  • The keyword return is used to pass data out of the function. Here the value stored in result is passed out or "returned."

Step By Step Videos

Using Multiple Parameter Variables: A function to add two numbers


Libraries

text

Libraries contain existing functions that has been created for you by professional programmers. The library that you are most familiar with now is the System.IO library that allows you to easily save data to a file and reload it. Software developers refer to libraries as APIs.

BBC Bitesize - Sub Programs


GCSE Exams

text


A Level: Passing data by reference compared to passing data by value

So far when we have passed data into a function as a parameter variable, we have been passing it in by value. When you pass data into a function by value it actually creates a copy of the data. This makes the variable a local variable. You can go ahead and change the value (e.g. Age in the above example). But if you change the value it only changes in the local copy and does not change the original data in the main program (when control returns to the main program after the function has executed).

You can also pass data by reference. If you pass data into a function by reference it will change the actual contents of the variable in the main program (when control returns to the main program after the function has executed).

Example code

Look at the AddToNum() function below. The parameter variable num is passed by value. So even though the function adds one to num, this will only change a local copy of the variable. The function calling AddToNum() passes data using the variable val as the argument variable (data being passed into a calling function). Once the AddToNum() function has executed val will still be 9 as only the copy of the variable changed (inside the AddToNum() function).

public void AddToNum(int num)
{
    // add one to num
    num++;
}

public void DoSomething()
{
    int val = 9;
    AddToNum(val);
}

Now look at the following code. Here the parameter variable passed into the function AddToNum() is passed by reference using the keyword ref. This means that the parameter variable num is still referencing the variable val that was passed in as an argument variable when the function was called. Thus num is not a local copy of the variable. This means that value of val will actually change from 9 to 10. Not that you must use the keyword ref hen calling the function infront of the argument variable.

public void AddToNum(ref int num)
{
    // add one to num
    num++;
}

public void DoSomething()
{
    int val = 9;
    AddToNum(ref val);
}

Why would you want to pass by reference?

Passing by reference takes up less memory because it doesn't make a copy of the variable. Also, since you can only return one value out of a function it is way to pass back more than one updated value.