Course 2 – Program structure

About functions

It’s time to talk about the structure of an application. I already told you that C++ is a compiled language – the code we write is transformed into machine code (0 and 1) and placed in a file called executable/binary. When we open the binary, it must start from somewhere – in C++, execution starts from a function called main.

A function groups together multiple lines of code, and is defined by the following:

  • Return type: The type returned by the function (or void, if we don’t return anything)
  • Function name: so we can call it
  • Function parameters: Variables which we pass into the function

Also, the content of the function need to be wrapped in brackets { }.

Let’s give an example:

Example 1

A function that returns nothing, is called myFunction, and receives no parameters.

Example 2

A function that returns an int, is called doSum, and receives two numbers (returning their sum).

Let’s go back to what I said before – the application starts from a function called main. This function returns an int, and receives no parameter.

Thus, the first program you can write could be this one:

Hello world!

The first program that the programmers are writing is the one that displays something on the screen. The code would look as such:

Let’s talk about this code, line by line:

#include <iostream>

This line allows us to add functionality in our code.

In the last course, I said the following: “Other than language-specific keywords, we also have access to the standard (STL) – a set of functions we can use, already created by the people that wrote the standard.”

There are many functions and classes (we’ll talk about those later) in the standard – and they are written in C++ files called headers (.hpp extension).

In order to have access to those functions, we need to add them in our code (to make them visible).

Basically, when we add the include line, when we compile the code, the compiler will take the content of that file and replace the include line with that content. Thus, everything that’s there will also be available in our application.

The header file called iostream uses input-output streams (thus the name) – or in other words, allow us to read input from the keyboard and show text on screen.

std::cout << “Hello world!”;

After we include the iostream file, we have access to an object called cout (character output device) from the standard (std) namespace. To use it, we need to use it’s full name (std::cout).

I don’t want to get into too much detail here – all the functions from standard are placed in a namespace called std, so there won’t be name collisions (functions with the same name) with the names we give to our variables and functions.

Like i said – we now have access to an object called cout. This is the way we interact with the screen.

If we want to show something on the screen, we need to use the insertion operator <<.

What we send after that operator is sent to the screen.

OK, now we know how to write stuff on the screen. Let’s talk about how to fetch data from the keyboard also.

std::cin >> data;

Similar to cout, we also have access to an object called cin, from the same namespace. To use it, we take input from the keyboard and send it to a variable, thus the operator is the other way around (from the object to the data >>).

If we want to send the value 5 to an integer, we write the following code:

Ok, now we know how to use cout and cin.

Conditional statement: If, else if, else

The next step would be to talk about conditionals: if, else if, else.

As an analogy:

  • If it’s raining outside, i’ll take an umbrella with me.
  • If it’s not raining, but it’s snowing, i’ll take my jacket.
  • Else (not raining, not snowing), i’ll take my hat.

Let’s say we want to check if a variable’s value is positive or negative.

Repetitive statement: while

Let’s talk about the while statement.

While is a repetitive loop (as long as the condition is true, the code inside will be executed).

As an analogy:

  • While it’s raining, i’ll keep the umbrella above my head.

This is an infinite loop – the code will be executed and the application will be stuck displaying that message – because the bool value will always be valid (True).

If we want to exit that function, we need to set the value to false.

Another solution would be to use the break keyword, to immediately leave the current loop:

You may also like...