Lesson 11: Functions
In our last lesson we took a look at conditional statements. In this lesson we’ll take a look at functions, which will let us develop better, larger, and more flexible code.
What are Functions?
Functions are essentially a programming structure that takes in input and returns output. Functions are also called procedures and subroutine, all of which refer to essentially the same thing.
You’ve likely seen functions before, in a mathematical context, in school. I think I remember seeing them as early as third grade. Have you ever seen an illustration like this:

Function
where you are give some values pairs such as x = 5, y = 7, x = 7, y = 9, x = 9, y = 11 and you are told to figure out what the function is (in this case, it’s be y = x + 2). If you have, that’s a function, in a form often given to elementary students.
Later on, probably in high school algebra classes, you usually see something such as f(x) = 5x + 1, where you send in an x and get an answer. So, f(5) = 5(5) + 1 = 26.
Functions in programming are very similar. The only real difference is that you can send in zero or more arguments (depending on your function). These values are called “parameters”. Also, a function doesn’t necessarily have to give a value back (some languages call these procedures or subroutine, to differentiate between functions which return values).
Anatomy of a Function
The exact anatomy of a function differs from language to language, but generally they have several specific. Here is an example function:
Function Integer sum(Integer a, Integer b)
Return a + b
End Function
Here is an example call to our function:
Integer total = sum(5, 6) Print total
Total would print “11″.
Name
In every language I know of, a function must have a name. This is the name used to call the function. In our math example, f(x) = 5x + 1, f would be the name of the function. In our example function, sum would be the function name.
Parameters and Arguments
Functions contain parameters and arguments. In our f(x) = 5x + 1 example, x is the parameter. It is the reference to the value which you must supply to use the function. The argument is the actual value that is supplied when the function is used, so if we use f(5), 5 is our actual parameter.
Using our example function, a and b would be our parameters. The parameters must both be Integers, as specified right before the parameter names. In our example function call, 5 and 6 would be our arguments.
Parameters are sometimes called “Formal parameters”, with arguments being called “actual parameters”. These definitions are more from the mathematical version of functions.
Body
A function must have a body, which is the actual code that that is performed when the function is called. In our example, the body is just one line, which is “Return a + b”. The body has no limit on the number of lines it can contain in it’s body (it can even be zero, though the function would do nothing). However, a good rule is if you can’t see the entire function without scrolling your text editor, it’s too big and should probably be broken down into smaller functions.
Return Value
The return value isn’t a requirement (a function usually can return nothing). However, usually if it does return a value it must be of a certain type (know as the return type). In our example, the Integer between the words “Function” and “sum” specifies that we return an integer.
We use the word “Return” and then put the value it actual returns, which is “a + b”.
Using Functions
Now that we know the parts of the function, let’s talk about actually making use of them.
Generally, you want to break your code down into functions so you can reuse the code. Our sum function is a bit too simplistic to make legitimate use of, so let’s come up with something more practical. Let’s say that we want to write our own power function (so we can do stuff like 2 to the power of 3).
To do this, we’ll use a for loop to multiple our value multiple times (remember 2 to the 3rd is the same as 2 * 2 * 2). We’ll name the function “power” and we’ll call our parameters x and y.
Function Integer power(Integer x, Integer y)
Integer returnValue = 1
For Integer i = 0; i < y; i = i + 1
returnValue = returnValue * x
Return x
End Function
Now, whenever we want to get a power value, we just call our function, instead of having to create a whole for loop each time.
Print power(5,2) Print power(2,3) Print power(3,4)
These lines would print "25", "8", and "81" respectively.
We've just used Integers, but you can use any type of variables. Also, a function doesn't have to return a value. Let's say you are working on a video game and you want to create a function to increase your score by 100 each time it is called, and if we total 1000 points, we should get an extra life. This wouldn't require you send in any parameters, but it would still do something useful.
Differences between Functions, Subroutines, and Procedures
In most languages, there is no difference between functions, subroutines and procedures, they just lump them all in as "function". In other languages, such as Visual Basic, they make a differentiate: functions must return a value while subroutines can't.
In this lesson we took a look at functions; their anatomy and their basic usage. In our next lesson we'll take a look at some of the more useful string functions.




