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? |
KEYWORDS on this page..
|
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:
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.
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.
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.
* covered in A Level Computer Science.
** 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.
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.
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 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.
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.
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.
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.
How do I pass data out of my procedure? Convert it into a funcion.
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.
Step By Step Videos
|
|
|
Using Multiple Parameter Variables: A function to add two numbers
|
|
Libraries
GCSE Exams
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).
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).
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.
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.