Learn To Code In C++ For Unreal Engine – Functions

Table of Contents

Learn To Code In C++ For Unreal Engine – Functions

Reading Time: < 1 minute
Level: Beginner
Version: Visual Studio 2019 Community Edition

Help Others Learn Game Development

It’s a common practice in programming to create a block of code for a certain operation, for example calculating two numbers, and then you reuse that code over and over in your project.

It would be a very tedious process to write those same lines of code every time we need them in the project, or copy / paste them every time.

This is where functions come into play.

A Function? What Is That?

A function is a way for us to group a block of code in one place, and then when we need that code we call it by using the functions name.
 
Let’s take the example from our previous post where we calculated two numbers.
 
Instead of writing the code for calculation on its own, we can create a function and put the code inside:
 
				
					void CalculateTwoNumbers()
{
    int a = 7;
    int b = 3;
    
    int result = a + b;
    
    std::cout << "The Result is: " << result;
}
				
			
The declaration of the function starts with the void keyword which denotes that the function doesn’t return a value, more on that later.
 
After that we give the function its name, and same as with variables the name is dependent on us and we can give it whichever name we want.
 
Of course the names are going to be meaningful so that you know the purpose of that function e.g. CalculateTwoNumbers. Then we open close parenthesis and open close curly brackets.
 
The code you want to execute goes between the { } of the function.
 
One thing that I need to point out since we are using the same .cpp file we are using from the start, if you declare this function below the main function then you will have errors:
 
				
					int main()
{

    CalculateTwoNumbers();

}

// Declared Below Main
void CalculateTwoNumbers()
{
    int a = 7;
    int b = 3;

    int result = a + b;

    std::cout << "The Result is: " << result;
}
				
			
If your code is like this and you run the app, this is what you will see:
 
Image 1 - 80%

The compiler is telling us that the build has failed which means this code will not run, and that the CalculateTwoNumbers identifier is not found.

The problem is that we declared the function below the main, and main doesn’t know that this function exists because it’s below it, so we need to move it above the main function like this:

				
					// Declared Above Main
void CalculateTwoNumbers()
{
    int a = 7;
    int b = 3;

    int result = a + b;

    std::cout << "The Result is: " << result;
}

int main()
{

    CalculateTwoNumbers();

}
				
			
And now we will not have any problems executing our code. If you run the app you will see this printed in the console:
 
Image 2
The result of the two calculated numbers is printed in the console and the code was executed by calling the function name e.g. CalculateTwoNumbers();
 
This is why functions are very important because they allow us to write any number of lines of code and then simply execute all those lines by calling the function name.
 
Just imagine how tedious it would be if we need to write or copy / paste 100 lines of code every time we need to execute a certain operation for our game.
 

Functions With Parameters

The previous function we created serves the purpose of introduction the concept of functions, but in terms of functionality it’s not useful.
 
The reason for that is because this function will always calculate the addition of two numbers, 7 and 3, and that’s it.
 
This is not something that you want in your game. This is where parameters come into play.

Let’s rewrite the function so that it takes two integer parameters:
 
				
					void CalculateTwoNumbers(int a, int b)
{
    int result = a + b;
    
    std::cout << "The Result is: " << result;
}
				
			
The function name didn’t change, but now we are not creating integers a and b inside the function, instead they are passed as parameters.
 
The parameters are added between the () of the function and they are separated by comma if you have more than one parameter which is the case here.
 
So now we can use a and b parameter inside the function itself and calculate their values by performing addition.
 
Now when we call the CalculateTwoNumbers function we can pass any two numbers between () of the function separating them by comma:
 
				
					int main()
{
    CalculateTwoNumbers(5, 33);
}
				
			
If you run this app you will see 38 printed in the console. Of course, you can change the numbers that are passed as parameters, and every time you will see their sum printed as the result in the console.
 
Also, you are not limited by the number of parameters that you can have inside the function and you are not limited by types of parameters.
 
Which means you can have 3 floats and 5 integers as parameters, or you can have a float, a string, a boolean and an integer as a parameter and so on.
 

Function That Returns A Value

Next type of function we have on the menu are functions that return a value.

The void in the function declaration tells the compiler that this function doesn’t return a value. 

And when I say value I mean of the variable types. So when a function returns some value, that value is going to be a float, integer, boolean or a string.

Let’s rewrite our CalculateTwoNumbers function:

				
					int CalculateTwoNumbers()
{
    int a = 3;
    int b = 7;
    
    return a + b;
}
				
			
As you can see the void part is now replaced with int, which means that this function will return an integer.
 
Inside the function we created integer a and b and we return their sum. Right away you can assume that we use the return keyword to denote that we are returning a value.
 
And we don’t have to create a third value that will store the sum between a and b, we can return a + b directly and it will calculate their value and return it.
 
But what does returning a value actually mean?
 
Well now we can do this:
				
					int main()
{
    std::cout << "The sum between a and b is: " << CalculateTwoNumbers();
}
				
			
If you run the app you will see 10 printed in the console. As you can see the function itself returns an integer.
 
This can of course, be modified so that the function returns a float, string or a boolean, you are not limited to having only integers as return values or parameters, and I already mentioned this.
 
We can also do this:
 
				
					int main()
{

    int result = CalculateTwoNumbers()

    std::cout << "The sum between a and b is: " << result;
}
				
			
If you run the app you will again see 10 printed in the console but this time we created a variable that will hold the value returned by the function.
 
After we store that value we can manipulate it how we want.
 

Function That Returns A Value And Takes Parameters

The previous function is good for the sake of example, but it is hard coded and it will always return the same number.
 
Let’s change that:
 
				
					int CalculateTwoNumbers(int a, int b)
{
    return a + b;
}
				
			

Now this function is flexible as it gets. It takes two parameters and then calculates the addition of those two parameters.

This means that we can pass any two numbers every time we call this function to execute:

				
					int main()
{

    std::cout << "The sum between a and b is: " << CalculateTwoNumbers(10, 20);
    
    std::cout << "The sum between a and b is: " << CalculateTwoNumbers(1, 2);
    
    std::cout << "The sum between a and b is: " << CalculateTwoNumbers(3, 3);
}
				
			

If you run the app now you will see 30, 3, and 6 printed in the console.

But you will see it like this: “The sum between a and b is: 30The sum between a and b is: 3The sum between a and b is: 6” – the printed lines are not separated or printed one below another.

We can fix that by adding “\n” in front of the second and third line inside the cout:

				
					std::cout << "The sum between a and b is: " << CalculateTwoNumbers(10, 20);

    std::cout << "\nThe sum between a and b is: " << CalculateTwoNumbers(1, 2);

    std::cout << "\nThe sum between a and b is: " << CalculateTwoNumbers(3, 3);
				
			
If you run the app now you will see all lines printed one below another. The \n will tell the compiler to print the line in a new row e.g. below the previous one, and this is better for clear visibility.

As you can see there are different ways how we can declare functions and based on its declaration the function will serve a different purpose.

But the point is that we can group the code we write, no matter if it’s 10 or 1000 lines of code, inside a function and whenever we need to execute that code we simply call the function name.

This way our code is more readable and more performant. We will see a lot of examples of using functions when we start building our games.

Where To Go From Here

In this post you learned how to group your code into functions for easier maintenance and calling. You also learned about the different types of functions that you can create.

To continue on your learning journey you can take a look at our Conditional Statements Lecture which is the next lecture in this tutorial series.

Leave a Comment