Layout and purpose of a full program

Responding to “what I believe each part of a simple function does” for the unreal course challenge.

The #include iostream preprocessor directive adds a library of a previously created file into the program that is being created. This library has information, such as variables, that can be used to more easily and concisely make the desired program, though I am not sure what specific information is contained within iostream in particular.

The main() function is run once in every program and is usually the focus through which other functions are called to influence how the program is made. Multiple functions can be made in the same file as main or they can be made in another file, then they are all called inside the main() function in order of how they are to affect the program. The main() function is then a concise, ordered thesis of the entire program that can be viewed to ascertain the purpose of the program and to narrow down where any errors may occur.

Expression statements and declaration statements are both statements made in the body of the program followed by a semicolon. It seems that declaration statements initialize a value to a variable, but expression statements are more difficult to narrow down. Apparently they can output to the terminal (and I assume ask for input from the terminal as well), but they can also assign a value to a previously initialized variable as well. The latter seems more the job of a declaration statement, so as of now, I’m still unsure of the main difference between the two.

The return statement is the final line of code in any program. It is not necessary in modern C++ programming to create a program, but it is preferred by most programmers for clarity. I believe it signifies the end of the program and may be able to verify if the program works as intended, but I am mostly unsure of its purpose or usefulness.

1 Like

The include doesn’t actually include the library. The #include preprocessor directive directs the compiler to include the content of the header when compiling to ensure all necessary definitions are in place for successful compilation.

C++ compilation is actually a 2-part process, the first is the compiler and the second is the linker and it is this part that packages your code with the necessary libraries. It is often necessary to configure this separately from include to combine the libraries with your application. In the case of the likes of iostream, this step is automatic.

I hope this clarifies things.

Privacy & Terms