| Programming Tutorial - Part 4 |
A program that Does SomethingYes – we’re going to write a new program to actually Do something now. There’s not much you can do with standard C, but we’ll give it a go. Types and variablesVariables are pretty crucial to any language, and C is no exception. Unlike other languages it’s quite important to understand how memory works to understand C variable types. So here’s a quick refresher. Memory on any computer is just a big list of numbers. Each number is called a byte and each byte has an address – a unique value that identifies it - usually counting up from 0. A byte is made from 8 binary digits (bits), and can hold 256 different values (2 to the power of 8 – 28). To store anything useful you usually need to group bytes together so that they can hold more information. For example, 2 bytes together can store 216 = 65536 different values. In every version of C you get the following different data types built in (all descriptions are for a standard PC program – other computers use different sizes):
Finally another programOk - we've got a good grounding in some basic things. Time for another program... Open up Visual Studio. On the startup window (or under File/Recent Projects->) you should find the project we made last time. Open it up and open Main.cpp (if it doesn't pop up) by double clicking it in the Solution Explorer on the left. Start by deleting everything there and paste in: //----------------------------------------------- for (loop=1; loop<11; loop++) Wow! So much STUFF! Hopefully the comments explain what's going on fairly well, but I'll break it down into bits like before. The first bit - the function SquareItLike all good functions this starts with a lovely comment. As you can see, Visual Studio has coloured the comment (and other words) to make them easier to see. I tried to get the colours into here, but this editor doesn't let me. Ho hum Anyway - this line : int SquareIt(int value) tells the compiler: "SquareIt is a function that returns an integer (the first 'int') and accepts one integer as a parameter that will be called 'value'" This is the start of a new block of code, not a statement, so wo don't finish with ; but use { instead. (Remember - the program will start running with a function called "main", so SquareIt won't run until we ask it to. All this stuff is just letting the compiler know about the function should we choose to use it) int squared; Next we define a variable called "squared" that's an integer. We do this by saying what the type of the variable is, and what it's called. If we don't fill it with something (as we haven't here) then its contents will be totally random. This is a statement, so finishes with a ; squared = value * value; Next we store 'value * value' in 'squared' with the '=' command. return squared; Finally we return the result and finish the 'body' of the function. Hurrah! We've calculated the square of a number! Or have we? Well - it depends what the number is. 'value' is integer, as is 'squared' and the return value of the function, so this is clearly not going to enjoy calculating the square of 0.5. "int" can be positive or negative though, so we can calculate the square of -100. However, if you square anything bigger than 46340, the result won't fit in an integer any more. But let's worry about these things later - on to the next bit! The second bit - the main functionNow we come to the "main" function again. This is where the program will actually start, so it's a good place to follow through if you want to know what's actually going to happen. int loop; int total = 0; First of all we define 2 variables. The first (loop) will have an undefined value. The second (total) will be initialised to 0. for (loop=1; loop<11; loop++) Ooh - what's this?! Well - it's a "for" loop - it starts a new block of code (see the { and } ) that will get repeated a few times - but the syntax is a bit odd. In a more expanded form this could be written something like: Set "loop" to be 1 For all the time that "loop" is less than 11: <do some stuff> Add one to "loop" and repeat The start of a loop like this has to take the form: for ( <what to do at the start>; <what to check>; <what to do each time round>) So our loop will start by running "loop = 1" before anything else. Then it will check if "loop < 11" (is loop less than 11? it certainly will be to begin with because we just set it to 1!). This is called the loop condition. If the check succeeds, then the code in the following code block will run. Then the "what to do each time round" code runs and the loop condition is checked again. The last piece of the puzzle is "loop++". This is a shorter way of writing "loop = loop + 1", and is used all the time in C. It will add 1 to loop after running the code block. Taken altogether, our "for" loop will run 10 times with "loop" taking the values from 1 to 10. When "loop" reaches 11 the condition "loop < 11" will fail, and the loop will exit before running for that 11th time. The last bit - calling SquareIttotal += SquareIt(loop); This line does two things. First of all it calls the function we made earlier, passing in "loop". "loop" is an integer and SquareIt is expecting an integer, so everyone will be happy. Next this line adds the returned value to "total". This is another shorthand a little like ++ - it means "total = total + SquareIt(loop);" and is also used a lot. And that's it! You've calculated the sum of the squares of the numbers from 1-10, and you have *no way* to find out what the answer is! SummaryTo conclude this part, what have we got so far? Well, believe it or not, but you've covered a large proportion of the core of C. We've still got to learn about a lot of things, but this stuff will make up a lot of all programs you write. We still need to learn about: - Using libraries. It would be nice if our program could *do* something. Print the answer, perhaps? - Using more than one source file. - Debugging programs and error messages - what's going wrong? - Extra data types - arrays, structures, pointers... Lovely! - C++. C++ adds a whole lot more to the mix, but everything here is still applicable. See you next time! |
||||||||||||||||||||||||||||
| Last Updated on Saturday, 06 September 2008 21:16 |









