Physics and Astronomy |
Back to top
Scientific programming in C: functions organise our codeThis should be mainly revsion. Seven plus or minus two?In the 1950s George Miller realised that people could typically remember about 7 (±2) things in their short term memory. Interestingly, he also pointed out that seven seems to be a typical limit in other slightly different memory-based contexts. In practice this means that anything we can't express in around seven independent concepts is going to be very difficult to deal with.Miller also pointed out that we are able to mentally organise more complicated sets of thoughts or things by organising them into groups: with a little practice most of us can remember and think about seven groups each of seven things even though we can't handle all forty-nine together at one time. Whilst it's easy to over-simplify this, the basic idea is valid: we can't handle complicated things and we have to organise them into independent chunks. In this context, "independent" means that when we are thinking about one chunk, or function, we don't have to think about the other functions. The square lawAn early study by IBM showed that the time taken to get a program working was proportional to the square of its size. This make sense if we think of the major task as being debugging: if the program is twice the size there will be twice as many mistakes and each mistake will take twice as long to find.Functions can help here by allowing us to debug one indepedent chunk at a time. Aside: Complexity is increasing not decreasing
Functions
Functions break large computing tasks into smaller ones, and
enable people to build on what others have done instead of starting
over from scratch. Kernighan & Ritchie.
Functions should be short and sweet, and do just one thing...
and do that well.
Structuring functionsImagine our task can be split into three sensible functions each of which can be split into two or three subfunctions for a total of eight subfunctions.. There are two ways to do this:Bad structure
Proper structure
Code that's used more than onceA related use of functions is that some tasks are required from many parts of the program. The most obvious examples are the standard library functions such as printf(), sin(), etc. We also will write code that will be used several times and that also should be a function. Avoid copy and paste!Sadly, when marking student work one often sees large amount of repeated code. It's quite clear what's happened: write a section of code; copy-paste, change all the "1"s to "2"s.Don't use copy-and-paste, use cut-and-paste to make it into a function. Problems with functionsTrivial functionsWe have already stated that functions should be non-trivial. As an example of a trivial function consider the following:/* * Add two numbers together */ float add_two_floats(float num1, float num2) { float sum; sum = num1 + num2; return sum; } This is bad because it is simpler and clearer to just write: z = x + y;rather than: z = add_two_floats(x, y); Functions that do two thingscalculate_area_of_a_square_and_check_the_spelling_of_some_text_ Side effectsA side effect is where a function that is advertised as doing one thing also does something else ("I know I'll always want to do it so I might as well do it here").Hidden assumptionsThis is the converse of a side effect: we implicitly assume that something else has already been done without actually saying so. If this is totally unavoidable then we should :
Interface errorsThe problems in the section above are essentially beginners errors. Interface errors are the big one.An interface error is where we are making a slightly different assumption inside a function that in the function we are calling it from.
Examples of interface errors
Tests for interface errors
SummaryA function should :
SummaryThe text of each key point is a link to the place in the web page. |