How to run your first program

How to run your first program, chapter 7

In this chapter, we’re going to write and run our first program. It’s a sort of tradition, whenever you learn a new programming language, to run a program that says “Hello world!”.

As basic as it sounds, it covers a few things. It makes sure that you have correctly configured things on your computer to run a program in that programming language. It also makes sure that you have a way to print, or display some data on your screen. Printing will be something that you do often when you’re developing.

Setting up the environment

We’re going to start off by installing some things. As I’ve mentioned before, I want you to get a taste of both dynamically typed languages and statically typed languages.

To do that, I’ll usually be showing both Python and C++ examples. Though sometimes, I will only show Python code because C++ can get complicated in some areas, and this is a software engineering series, not a C++ one.

Feel free to choose whichever language you want to follow this series with, but do read what I say about the other language! As a software engineer, it’s really important to know a bit of everything. This is because it allows you to understand seemingly unconnected concepts at a deeper level.

Python setup

Python 3 Interpreter

Most computers come with the interpreter of Python already installed. However, I still recommend installing it again, or a new version.

You see, Python is usually split into two major versions: Python 2 and Python 3. We will try to stay with Python 3 because it’s the newer and recommended one. The differences between the two are not really important, but libraries (that code written by other developers that you can import and use in your own code) tend to be written for either Python 2 or 3. That’s also not that important to know, but at least you get an idea that code written on one of the major Python versions is not always compatible with the other.

So, to install Python 3, just go to their official website, hit download and install it!

Obviously, each operating system has its own installation process, but usually if you leave the default configuration during the installation, you usually install it in the right way.

Python Code editor

Once you have installed the Python 3 interpreter, you need a program to actually write your code in. For Python, I recommend the one that I use: Visual Studio Code.

To install it, go to their official website, hit download and install it. Once installed, I recommend opening it and installing the Python extension from the extensions marketplace (you can find it in the sidebar).

C++ Setup

C++ compiler

Your computer might come with a C++ compiler, but you should probably make sure you do have it, or install one anyway.

Now, I need to make a side note. If you go down the C++ path, you will hit a lot of walls when configuring things. This is normal. The one thing software engineers absolutely hate is configuration. This is your chance at developing those exploration skills we’ve discussed in the last chapter.

The instructions to install the C++ compiler depend on your operating system. For this, I recommend searching on a search engine “How to install the C++ compiler for mac/linux/windows”. I just did, and found this page. Maybe you want to try it out, or do your own search.

C++ Integrated Development Environment

To write your code and run it, I recommend starting with an integrated development environment (IDE). This is a software that does a lot of things for you. Though, since it does a lot under the hood, it also requires more complicated configuration. Really, it’s not that bad, but you will probably need to search how to run a C++ program on it.

The one that I recommend (and is free) is Eclipse. You can download it on the official website. Installing it and running your first C++ program won’t be exactly straightforward. I really recommend searching for a tutorial post or video. This is, again, crucial to develop your exploration skills.

How to write your first program

We are going to write just a few lines of code. They are going to print the message “Hello world!” on our screen.

Writing the Python program

In VS Code, create a new file named anything that you want with “.py” at the end (for example, hello_world.py).

Inside the file, write the following line of code:

print("Hello world!")

That’s it! This line calls, or executes, the print function. It gives it one parameter, or argument, a string of characters with a value of "Hello world!".

Okay, we haven’t learned about how exactly functions work, so don’t think too much about the last paragraph. What I wanted to do is to show you a bit of the vocabulary used by developers (and one you will see when you do your own research).

Before we run this code, let’s look at the code in C++.

Writing the C++ program

In Eclipse, create a new source file in your project. Name it anything you want with “.cpp” at the end (for example, main.cpp).

Inside the file, write the following code:

#include <iostream>

int main() {
    std::cout << "Hello world!";
    return 0;
}

It looks a bit more complicated than Python, right? Let’s look at what each line does.

The first line includes a library called iostream (or input/output stream). This is a library, which among other things, gives us access to the function cout (or console output). In Python, we didn’t need to include a library for our print function, because Python automatically gives you access to it.

We also see a function definition. More specifically, we see the definition of the main function. This is where every single C++ application starts its execution. Again, since we haven’t learned about functions yet, it’s best that you take it as is. It will make sense soon! The same applies to return 0;.

Finally, we see the cout function getting called with "Hello world!". This is the function that prints something on our screen.

How to run your first program

Running the Python program

To run the Python file you’ve written, VS Code provides a run button. If you don’t see it, you can run it from the “Run” menu at the top.

If you don’t manage to run your Python program, try searching for something like “How to run a Python program on VS Code”.

What you should see after running the program is a console pop up with the text “Hello world!” inside.

Running the C++ program

To run the C++ file, you need to do two things: building the project and running it.

Building the project is the action of gathering all the libraries that you need (in our case iostream), and compiling our code. Building can be done in the “Project” menu at the top. After building the project, you can press the “Run” button to run it.

If you encounter any problems building and running your C++ file, you should search for something like “How to run a C++ file in Eclipse”.

What you should see is, just like for the Python code, a console pop up with “Hello world!” written inside.

You’ve run your first program

Congratulations, you’ve gone through the “Hello world!” rites of passage!

Maybe you even made your debut in exploration by searching for a few things that didn’t work during your setup. You should continue doing this, as well as looking around your code editor or IDE. Most things probably don’t make sense yet, but don’t be afraid to look.

Now that you have run your first program, we can dive a bit deeper.


Next: Chapter 8 – What data types are there?

If you want to start from the beginning of the series, go to chapter 0.

By Radu

Software Engineer and Computational Scientist graduated from the Technical University of Munich. Worked at Shopify, DRW and Ubisoft.

Leave a comment

Your email address will not be published.

Join my mailing list to get an email whenever a new chapter comes out!