Moved to Wordpress

This blog has moved to Wordpress. GOTO the new blog now.

There will be no new updates here. Most of the posts have also been expanded.

2011/01/29

What In The Hell Are Functions

Functions go by many names depending largely upon the programming language you use. You may hear them referred to as subroutines, procedures, methods, routines and probably more. There are many names to describe functions, but in the end they all boil down to just about the same thing. A function is a method of grouping a set of instructions to preform a certain task.

The simplest way to explain this concept is with an example. Suppose you wanted a program that displays the procedure for getting the area of a circle. First let’s explore the most straightforward way to achieve this.



When you run this program it will output:

The formula for the area of a circle is Pi*r2
The radius of the circle is: 5
The area of the circle is: 78.5

OK. We’re done. But wait, what if you want to show the area of more than one circle? Three circles you say? Ok that’s easy, just cut and paste the code above three times and we get the following program.




Which will print out:

The formula for the area of a circle is Pi*r2
The radius of the circle is: 5
The area of the circle is: 78.5

The formula for the area of a circle is Pi*r2
The radius of the circle is: 10
The area of the circle is: 314.0

The formula for the area of a circle is Pi*r2
The radius of the circle is: 10
The area of the circle is: 314.0


That wasn’t too bad. But what about 1,000 circles? That certainly is a lot of copying and pasting. Fortunately for us there is a better way. It is called a function.

If you look at the code that we copied and pasted above you’ll notice a lot of similarities. We’ll use these similarities to create a function called circleRadius to print this information for us. Our new program now looks like this:




And the output is exactly the same as last time:

The formula for the area of a circle is Pi*r2
The radius of the circle is: 5
The area of the circle is: 78.5

The formula for the area of a circle is Pi*r2
The radius of the circle is: 10
The area of the circle is: 314.0

The formula for the area of a circle is Pi*r2
The radius of the circle is: 15
The area of the circle is: 706.5


The code using the function is a lot shorter than the code we copied and pasted. It is also a little more clear than the previous version. However the real benefit comes when we want to make a change to how the information is displayed. Suppose that you just wanted to print the radius and the area, like this:

radius = 5
area = 78.5

It would take you quite a lot of work to change all those copy/paste versions to make this work. But with a function you only have to change it in one place and it will work all over. Our new version looks like this:




And the output is:

radius = 5
area = 78.5

radius = 10
area = 314.0

radius = 15
area = 706.5


Notice how the “circleRadius(x)” has not changed? The power of functions extends well beyond this small example, but this should get you started writing smaller, modifiable and reusable code.



Check back for articles that cover some of the more advanced features of functions, including recursion, nesting and closures.

No comments:

Post a Comment