Sun. Jan 22nd, 2023


In addition to the builtin functions provided by programming languages, a fundamental skill is the ability to define and use your own functions.

Reasons to Use Functions

When you plan code, whether using flowcharts or pseudocode, you will hopefully be segmenting your code into blocks – this makes it easier to visualise and keep track of.

In your code, you should maintain this structure. Well-planned code is modular: that is, made up of separate blocks that each do one job, and are linked together. Not only does this make coding easier, it makes it easier to maintain in future, because you will have given each function a name which reflects its purpose. It’s like having comments added that describe what it does, for free!

Another benefit arises when you need to run the same piece of code many times. Imagine you have a calculation that needs completing, but it only requires four lines of code to do this. So you think you can get away with copying and pasting the four lines every time you want to do it. Not only has this made your code much longer than it needs to be, now imagine you need to change something – maybe you made a mistake, or the requirements have changed. You will be left having to find every occurrence of that calculation, and will have to correct or change every single one.

Defining that calculation as a function means you will only have to find and correct or amend the calculation once – far easier, and far less error prone (no danger of missing an occurrence).

Defining a Simple Function

To define a simple function, you need to provide it with a name and the code that should execute when that function is called (used).


//Define the function
void PrintMessage()
Console.WriteLine("This is the message that gets printed")

//Use the function


#Define the function
def PrintMessage():
print("This is the message that gets printed")

#Use the function

Note: in Python you must declare the function in the code before you can use it (i.e. above any use of it). This does not apply to C#

Declaring Arguments

Imagine we are building a calculator, and we need a function that adds two numbers together. We will need to be able to tell our function what those two numbers are. To do this, when we define our function, we will also tell it to expect two arguments, by naming two parameters.

This is less confusing than it sounds: the definition of the function will indicate that two parameters are required (we will call them a and b); when we use the function and actually supply values for a and b, those values are called arguments.

// Define a function called AddNumbers with two parameters, a and b
void AddNumbers(int a, int b)
c = a + b;
Console.WriteLine("The answer is " + c);

//Call the function with the arguments 4 and 6.
AddNumbers(4, 6);

#Define a function called AddNumbers with two parameters, a and b
def AddNumbers(a, b):
c = a + b
print("The answer is " + str(c))

#Call the function with the arguments 4 and 6.
AddNumbers(4, 6)