C++ Tutorial #11 – Functions

Facebooktwittergoogle_plusredditpinterestlinkedinmail

This is where programming really gets interesting. We are going to write functions in our program today. Functions allow us to design programs in sections of code to perform specific tasks. In our program, we will be creating functions for finding the area of 5 different types of shapes.

 

Functions are used a lot in game programming: They are used to load content such as graphics and sound; update changes that happen such as player movement and ai; and they are also used to draw the content on the screen. We will talk more about this in another tutorial; it’s just to give you an idea of what functions are used for. Lets get started, this is really fun and easy to do.

 

Here’s the syntax of a function:

 

type name_of_function(parameter1, parameter2, etc.)
{
statement;
}

 

 

Here’s how a function works:

 

type – This is where we place the type of value that our function will return.
ex. int, float, string, char, etc.

 

name_of_function – This is the name of the function. You can give it whatever name you want.

 

parameter – This is just like a variable declaration, and acts inside the function like a regular variable. You can use as many parameters as you need in your function.

 

statement – This is the body of the function. This is where you write out what your function does

 

Lets write our code:

 

 

 

Here’s what our program looks like:

 

25
15
12
36
12.5664

 

Let me explain the code:

 

This is new to you, so don’t panic! This is called a Preprocessor Directive, we will use more of these in future tutorials. For right now all you need to know is that we start out by writing “#define”, giving the identifier a name (pi), and assign it a value. Another thing I want to point out is that you can’t change a value of a preprocessor directive.

 

 

 

In this section of our code, we created five functions: area_of_square, area_of_triangle, area_of_rectangle, area_of_parallelogram, and area_of circle. If you look inside of each function, you’ll notice we created a variable inside of it. Then we used the parameters inside our function to make our formulas, and return our answer. Notice how we used float instead of int. We used float in our program so we can also use decimal numbers in our functions.

 

 

 

 

Inside our main function, we used cout to print out the return value of the function. Take a look at area_of_square, it has one parameter: float length. we put 5 in place of that parameter. Then the program goes to that function and does the math that we wrote. Since the area of a square is length times length (5 * 5), the function returns 25.

 

 

 

 

I’m going to end this tutorial here. We will discuss more on functions on another tutorial, because there is more to learn. This is just the basics of writing functions. Here’s a simple challenge: Try to use what you learned. and make functions that convert yards to meters, inches to centimeters, or miles to kilometers. Try other units like weight, volume and temperature. Good Luck!

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *