|  | 
 
 
 
 Modular programming is a style that adds structure and readability to your program code. It may not make much difference on small projects, but as you start to work on something bigger it can make your code much easier read and maintain, so it's worth getting into good habits now. Structuring your code is a simple task of splitting your program into manageable chunks so that each part is self contained. By creating these self contained modules, you can focus your programming to each part. Once you are satisfied that one bit is working, you can then move to the next module with the confidence that whatever you do elsewhere will not have a knock on affect on other modules you have already written. Blitz Basic gives us three methods for modular programming: 
 Functions & Subroutines
 To illustrate this, let us look at a simple example: The textBox() function is basically an extension of the Text command but in addition to drawing some text, it also draws a box around it. You may use something like this to create a simple menu. Rather than duplicate the code for each option, we just make one simple call to the function and it does the rest. Not only is this a much easier way of programming, but you can use this function in any program you write. This is when you start creating your own libraries (see below for details). Functions vs Subroutines
 
 Include Files
 This is a simple example of splitting your source code where you have one include containing your generic library functions (see below), one with your game functions and one which loads your images etc. Note that you must load images AFTER the Graphics command since all current images are unloaded when this command is used. You only need to compile your main file and any include files will be inserted during the compilation. Organising Your Files
 A library is a collection of generic functions that can be used by any program. Each library is a separate .bb file which can be included in your source if any of it's functions are needed. As explained above, I recommend you have one directory where you keep all your generic libraries so all your programs can reference the same files. To get you started, my own libraries are available on my site and there are also many other libraries available on here on www.blitzcoder.com and on www.blitzbasic.com. Creating Your Own Libraries
 A few tips for writing your own libraries: 
 Optional Parameters
 
 To declare an optional parameter, you simply specify a default value for that variable as shown in the example below. Optional parameters must be declared after the required values and once you have specified one optional parameter, all parameters following this must also be optional. Lets add some more parameters to our example: As you can see, we have added two extra parameters to centre the text at the x position (instead of starting at the x position) and to change the gap between the text and the border. When calling this function and you want to specify the gap, you must specify the centreText parameter too, even if you are using it's default value, since it is declared before the gap parameter. Some Other Useful Tips
 Also, notice the indentation. This is used to distinguish between levels of nested code. This also helps make your program more readable since you can easily see where constructs like loops & If statements begin and end. Well, I hope this has been helpful for you. Happy coding... This article can also be downloaded from my site: www.aurora-soft.co.uk For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |