A bluffer's guide the previous post
Mar. 21st, 2011 01:48 pmA brief explanation of the magic runes for complete non-programmers
It's probably a futile hope that there can ever be a clear connection between "things I wrote at 1am when I was drunk" and "a coherent explanation of abstract concepts in simple terms for people who've never heard them before" :)
But in the unlikely event that anyone both (a) had difficulty understanding the original example program and (b) is curious enough that they would LIKE to follow some of this series, here's a very very brief introduction.
int main() {
...
}
For Your Very First C++ Program Ever just accept that this is the start and end of the code that actually does something. (There may be some configuration before and afterwards.) The concept of what it actually is quite simple, but not necessary for Your Very First C++ Program Ever. Other similar languages will have similar -- but different -- ways of showing "which bit of the code should actually run". (Some don't have anything -- the code runs from the very beginning of the file, skipping over any configuration.)
I could have instead chosen to simplify/complicate matters by not writing a whole program, but just showing the lines of code I was interested in. But I decided not to because (a) it increases the chance that I mess up and make something that isn't valid and it's hard to explain what's wrong (b) some people actually find it more intuitive to see the whole thing and know that (with enough effort) they could compile it into an .exe and run it and it would work, rather than knowing the inputs come from some hypothetical other part of the program that's never been written, and thence go the outputs too.
int square_size = 12;
This declares a variable called "square_size" containing a value "12" which we're going to do some calculation on.
Implicit in the idea of a sample program is that we might use a simple example like this if either (a) we wanted to perform a one off calculation. You wouldn't actually use a program rather a calculator to calculate an area but for a slightly more complex calculation you might or (b) you want to get the calculation working right, and then may come back, and instead of permanently specifying the input value, have it provided by user input, or by another part of the program that (for some reason) wants to calculate areas.
int square_area = square_size * square_size;
This actually does the calculation, multiplying square_size by square_size and putting the result in other variable called "square_area"
cout << "This is blah blah " << square_area;
This prints to the screen, first the string in quotes, and then the numerical value stored in square_area (144 or whatever). It doesn't matter for your first program to know exactly how "cout" and "<<" are used, just that "cout <<" means "send something to the screen" and further things are separated by further "<<".
That's probably less intuitive than something like "print 'blah blah', var", but most of the time, since printing to the screen is something so necessary for testing simple programs, but so complicted internally (multiplication is typically one machine instruction, changing the text on the monitor in the right place is LOTS), that for most languages you initially learn a syntax that gets things onto the screen but don't try to break it down further at first.
Here we print the result of the calculation to the screen. Several people expected us to use a "return" statement to pass the result of the calculation to some other part of the program, but I hadn't bothered to do that yet. Basically, the important code is just the line with the multiplication, the other is all a stand-in for the sort of code we'd have elsewhere, but a real program wouldn't have it exactly like that.
It's probably a futile hope that there can ever be a clear connection between "things I wrote at 1am when I was drunk" and "a coherent explanation of abstract concepts in simple terms for people who've never heard them before" :)
But in the unlikely event that anyone both (a) had difficulty understanding the original example program and (b) is curious enough that they would LIKE to follow some of this series, here's a very very brief introduction.
int main() {
...
}
For Your Very First C++ Program Ever just accept that this is the start and end of the code that actually does something. (There may be some configuration before and afterwards.) The concept of what it actually is quite simple, but not necessary for Your Very First C++ Program Ever. Other similar languages will have similar -- but different -- ways of showing "which bit of the code should actually run". (Some don't have anything -- the code runs from the very beginning of the file, skipping over any configuration.)
I could have instead chosen to simplify/complicate matters by not writing a whole program, but just showing the lines of code I was interested in. But I decided not to because (a) it increases the chance that I mess up and make something that isn't valid and it's hard to explain what's wrong (b) some people actually find it more intuitive to see the whole thing and know that (with enough effort) they could compile it into an .exe and run it and it would work, rather than knowing the inputs come from some hypothetical other part of the program that's never been written, and thence go the outputs too.
int square_size = 12;
This declares a variable called "square_size" containing a value "12" which we're going to do some calculation on.
Implicit in the idea of a sample program is that we might use a simple example like this if either (a) we wanted to perform a one off calculation. You wouldn't actually use a program rather a calculator to calculate an area but for a slightly more complex calculation you might or (b) you want to get the calculation working right, and then may come back, and instead of permanently specifying the input value, have it provided by user input, or by another part of the program that (for some reason) wants to calculate areas.
int square_area = square_size * square_size;
This actually does the calculation, multiplying square_size by square_size and putting the result in other variable called "square_area"
cout << "This is blah blah " << square_area;
This prints to the screen, first the string in quotes, and then the numerical value stored in square_area (144 or whatever). It doesn't matter for your first program to know exactly how "cout" and "<<" are used, just that "cout <<" means "send something to the screen" and further things are separated by further "<<".
That's probably less intuitive than something like "print 'blah blah', var", but most of the time, since printing to the screen is something so necessary for testing simple programs, but so complicted internally (multiplication is typically one machine instruction, changing the text on the monitor in the right place is LOTS), that for most languages you initially learn a syntax that gets things onto the screen but don't try to break it down further at first.
Here we print the result of the calculation to the screen. Several people expected us to use a "return" statement to pass the result of the calculation to some other part of the program, but I hadn't bothered to do that yet. Basically, the important code is just the line with the multiplication, the other is all a stand-in for the sort of code we'd have elsewhere, but a real program wouldn't have it exactly like that.