Hey there! Last time on Programming 101 we took a look at some of the concepts and lingo behind programming. If you haven’t read the first installment, you can check it out here. Today’s mini-lecture will cover less abstract terms, and I’ll be guiding you as you build your first program!
For the purposes of this guide, I’ve decided to use the C++ as my programming language of choice. C++ is great because it provides users with facilities for low level memory manipulation and higher level object-oriented functionality. Just so you know, procedural programming is all about executing code in a sequential order similar to how you would check off items on a list. On the other hand, object-oriented programming deals with constructs called objects. These objects act like the components of a car, each separate piece contributes and does its part for the whole, and the blueprints for these objects are known as classes. For the purposes of today’s blog, we will be strictly dealing with procedural programming.
The environment that we will be programming on is known as the Eclipse Kepler CDT. I found a great guide for beginners which takes you through all the steps necessary to setup the CDT. You can find the guide here.
I find that the easiest way to get into programming is to take it head on. With that said, let’s take a look at the syntax for the our first program:
using namespace std;
cout << “Hello World” << endl; // prints Hello World
First, the #include <iostream> above is an example of a preprocessor directive, which is basically an instruction to the preprocessor telling it to include the library known as <iostream> and all its relevant functions, symbols, and values. Next, using namespace std; is similar in the sense that using namespace is a command for the preprocessor to allow access to the library known as std, which is short for ‘standard.’
Moving on, int main () declares the main body of the program. The int part refers to the fact that an integer value will be returned at the end of the program and main() just tells Eclipse that this body of code is where any other user-defined functions can be referenced from.
To output to the console window, we use the cout function. In C++, and in many other programming languages, an instruction inside a function is terminated by a semicolon (;). Lastly, the return 0; stems from the fact that our main function was an integer type as mentioned before. A piece of code stops executing after the return statement and we will see useful applications of this in later installments of Programming 101.
That wraps up today’s Programming 101 post. Next time we’ll be looking at conditional branches, loops, different types of data variables, and other practical code.
Good luck, Code Warriors!