| Programming Tutorial - Part 3 |
Let’s Program Properly!A little background on the languageOk. Let’s start with C. Before we begin properly properly, I think we need to say a few words about how a C program hangs together. The C language itself is amazingly simple – there’s hardly anything to it. What sort of commands does it have? Well, in any C program you’ll only find a very few things: - Program structure. Blocks of program are split up with { and }. Lines usually have to finish with a “;”… That’s about it for structure. - Flow control. You can tell the program where to go with loop commands. - Simple maths. C knows about +,-,*,/ and a few others. - Variables. Variables are just pieces of data. C only knows about a few very simple types of data, but you can tell it about more complicated things. - Functions. A function is just a block of program with a header and { } around it – we’ll look at some in a minute. A function call is just a function name and some parameters in brackets. And… that’s about it. C doesn’t have any built-in commands at all. No “print” or “sound” or “line” or “cls” or any of the things you’d usually expect from a language. Nothing. So how can you program in it? Well, over the years people have written millions of functions in C that other programmers can use. Things like “sin” and “cos” and “print” and all those handy functions are usually just built using C and then made available to everyone else. The things that look like commands in C are actually just function calls the same as all the other function calls you’ll make. This makes C very easy to move from computer to computer – the language itself can run on *anything*. Neat, huh? In the library, with the lead pipe. All this does of course mean that in order to do anything useful you’ll need to either write a vast amount of code, or get someone else’s code to play with. Luckily your brand new Visual Studio install comes bundled with oodles of other people’s functions. The most important of these come in two different flavours: - The “standard libraries”. These are supplied with just about every C compiler out there and let you do things like read from files on disk, print text, take the sine and cosine of angles etc. The real “meat and potatoes” of any programmer’s “3 course programming meal”. - The Windows SDK (Software Development Kit). These functions let you write programs that look like all those other Windows applications out there. This is the real “duck’s bottom” of any programmer’s “3 course programming meal”. There are loads of other collections of functions out there. A Direct X SDK will let you write wonderful 3D games. A physics SDK will let you attach physics to anything you care to name. The world’s your oyster! Back to our Great GameOk. So let's take another look at our great game. What does it all mean? Well, there isn't that much - let's look at it line by line. Lines 1 & 2 - The start of the Function Definitionint main(){ These lines are the start of a function "definition". They are the start of a block of code that's going to define an encapsulated lump of Function. As we said before, every program has to define afunction called "main", and this is ours. All function definitions have 3 parts: - The "return value type". All functions return something, even if that something is nothing. Functions can return data of any type (see Types later), but this function is going to return an integer, so the type is "int". - The function name. Like all names in C this has to begin with a letter or _ and can contain letters, numbers or _. No symbols or spaces. Case is important, so main, Main, __main45, FreddyAndTheDreamers are all different (valid) names. This function is called (you guessed it) "main". - The parameter list. Functions can be sent data to work with - a set of *parameters*. These are listed after the function name between brackets. Our function has no parameters - () This function continues for the whole of the function block. The block was started with {. See if you can guess where it ends... Line 3 - The Content // Make a game. Give it good graphics. And great sound. This is where it's all happening! A comment. Comments in C or C++ can be either like this (with // at the start), or in blocks with /* at the start and */ at the end. E.g. /* This is commented and will be ignored */ Lines 4 & 5 - The end of the functionreturn 0; }This is where the whole rollercoaster ride comes to a sad end. Remember that our function had to return an integer value? Well, this is it. And as it's a command, we finish our return 0 with a ; like all good commands. Then we close off the block of this function, and our work is complete! |
| Last Updated on Thursday, 21 August 2008 21:00 |









