Program
In order to organise your code, programs need to become more than just a list of instructions with variables. In this chapter you will see how to organise your code into functions and procedures. This means that you now need to picture the concept of a program differently, as shown in the following concept map.
Program — when, why, and how
The role of the program has not changed — it is still the main thing that people will run. What changes now is what we can see within the program, as your programs will be built from a collection of functions and procedures rather than a long sequence of instructions. This change results in a new way of approaching program design. You don’t need to start with low level details like specific logic. Instead, you can break the problem down into functions and procedures.
In C/C++
This syntax contains some similar elements to the syntax you have already been using. For example, as with C#, in C/C++ you need to specify where the compiler can find any library code you are going to use. You can also declare variables and constants.
The major difference is that you can now also declare procedures and functions.
These are concepts we will explore shortly.
Additionally, for the file to contain a valid C/C++ program, you need to ensure that one of the functions within the program has the name main
, and that it returns an integer value.
Header include
The #include
code allows you to include files that contain declarations for the different things you are going to use within your program. When you include a file you gain access to the functions, procedures, constants, and variables declared within it. You can then either compile the code for these yourself, or link in an existing library.
This is similar to the using
statements you used in your C# code.
Using directives
One of the challenges in programming is uniquely naming things we create. To help manage this, programming languages have come up with a mechanism to create namespaces, which allow you to define the scope in which a name must be unique. The using
directive gives you easier access to particular items within another namespace.
In C/C++ the standard name space (which has the identifier std
) contains some useful functions. You can access these anywhere using the full name of the function, which is defined by the namespace, two colons, then the function identifier. For example, the to_string
function in the standard namespace can be accessed using std::to_string
. This will always work, but means you have to type std::
before to_string
each time. The using
statement lets you avoid this. By adding using std::to_string;
at the top of the code, you can now just use to_string
everywhere and the compiler will not complain, as you have told it you are “using” std::to_string
.
Example
Here is the code of a program with functions.
We have added comments to help you identify different parts of the syntax.
Don’t worry if you don’t understand all of this — we still need to explore most of these concepts.
For now, focus on what looks different and what looks similar, and try to match the different sections with the syntax diagram above.
Also take note of int main()
, which defines the entry point of the program.