Creating and Calling C++ Functions

An introduction to functions - reusable blocks of code that we can use to break our application into smaller pieces.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a technician character
Ryan McCombe
Ryan McCombe
Edited

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:

  • Move
  • Attack
  • Take Damage
  • Die
  • Drop Loot

To create these abilities, we create functions.

The 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.

Creating C++ Functions

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() {

}

White Space

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.

Test your Knowledge

How might we define a function called LevelUp?

Calling C++ Functions

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.

Test your Knowledge

How might we call a function called LevelUp?

In the next lesson, we'll see how we can navigate our functions using the debugger.

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Edited
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

The C++ Call Stack and Debugging Functions

An introduction to how our function calls create a call stack, and how we can navigate it in a debugger.
3D art showing a ladybug
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved