Earlier, we saw how we could think of objects as being combined of two things - properties, and behaviours.
Variables are how we create properties. These are descriptions of the current state of our objects, like how much Health
they have and what Level
they are.
Functions are how we define behaviours. Functions let our objects do things. Some examples could be:
To create these abilities, we create functions.
main
Function in C++You may have realised we're already working with a function in our code - the main
function:
int main() {
// ... code here
}
The uniquely named main
function is the "entry-point" to our code. When our code is compiled, the compiler looks for a function called main
. Yhis is the function that is called when our executable is run. When the main function ends, our program ends.
We can create as many functions as we want in our code. We're not just limited to the main
function.
Let's start by creating a simple function that modifies some variables.
In order to create a function, we need to decide on a name for it. The rules for naming functions are the same as naming variables. Aside from rules like not having any spaces, we can call our functions almost anything.
Just like with variables, we should aim to be descriptive with our function names.
Lets assume we wanted a function to have our monsers take damage. A good name might simply be TakeDamage
. We can create a new function above our main
function like this:
#include <iostream>
using namespace std;
void TakeDamage() {
}
int main() {
}
Aside from TakeDamage
having a different name to our main
function, we can also see that we have used the void
prefix rather than int
.
void
and int
refer to the data type that will be returned from the function. We will discuss return types in more detail a little later in this section.
For now, lets just ensure we always have the int
keyword in our main
function heading, and void
in every other function heading we create.
Lets add some variables to our code, and also fill in the body of the TakeDamage
function. The body is the area between the {
and }
. This is where we make our function perform actions, just like we have with the main
function in the past.
#include <iostream>
using namespace std;
int Health { 150 };
bool isDead { false };
void TakeDamage() {
Health -= 150;
isDead = true;
}
int main() {
}
Remember, the spacing of our code is not important to the compiler. When it comes to functions, many people prefer to put the opening {
on the line after the function name. That is also valid:
void TakeDamage()
{
Health -= 150;
isDead = true;
}
By convention, we will always indent the code within the body of the function, so it is offset from the left edge. This indenting is either done with the spacebar or the tab key. This has been shown in all our code samples so far.
Like with all white space, this is not important to the compiler, but it makes things easier to read for humans. Almost all editors will do this indenting for you automatically, as you may have noticed.
You can modify the settings of your editor to specify how big the automated indents should be, and whether they should be done with tabs or spaces.
How might we define a function called LevelUp
?
Just because we have created a function, it doesn't mean the code in that function will ever run. The only function that we can consider to run automatically is the main
function.
In order to execute the code within any other function, we need to call that function. This is sometimes also referred to as invoking the function.
We call a function using its name, immediately followed by a set of brackets, like this:
TakeDamage();
Given that we know the main
function is going to be run automatically, we can put the code to call our TakeDamage
function within the body of the main
function. This ensure it is called:
#include <iostream>
using namespace std;
int Health { 150 };
bool isDead { false };
void TakeDamage() {
Health -= 150;
isDead = true;
}
int main() {
TakeDamage();
}
Note, it is important that the TakeDamage
function come before the main
function in our code. Were we to switch the order of these functions, putting main
above TakeDamage
, our code would not compile.
The compiler reads our code from top to bottom so, when it sees us trying to call a function that we haven't defined yet, it will fail with an error.
We will soon see techniques that make the order in which we define our functions less important, but for now, lets just order our file such that we're always defining functions prior to calling them.
How might we call a function called LevelUp
?
In the next lesson, we'll see how we can navigate our functions using the debugger.
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way