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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// Functions Example #include<iostream> #include<conio.h> using namespace std; #define pi 3.14159 float area_of_square(float length) { float area; area = length*length; return area; } float area_of_triangle(float base, float height) { float area; area = 0.5*(base*height); return area; } float area_of_rectangle(float length, float width) { float area; area = length*width; return area; } float area_of_parallelogram(float base, float height) { float area; area = base*height; return area; } double area_of_circle(double radius) { double area; area = pi*(radius*radius); return area; } int main () { cout << area_of_square(5) << endl; cout << area_of_triangle(5,6) << endl; cout << area_of_rectangle(3,4) << endl; cout << area_of_parallelogram(6,6) << endl; cout << area_of_circle(2) << endl; getch(); return 0; } |

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

6 |
#define pi 3.14159 |

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.

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
float area_of_square(float length) { float area; area = length*length; return area; } float area_of_triangle(float base, float height) { float area; area = 0.5*(base*height); return area; } float area_of_rectangle(float length, float width) { float area; area = length*width; return area; } float area_of_parallelogram(float base, float height) { float area; area = base*height; return area; } double area_of_circle(double radius) { double area; area = pi*(radius*radius); return area; } |

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.

43 44 45 46 47 48 49 50 51 52 |
int main () { cout << area_of_square(5) << endl; cout << area_of_triangle(5,6) << endl; cout << area_of_rectangle(3,4) << endl; cout << area_of_parallelogram(6,6) << endl; cout << area_of_circle(2) << endl; getch(); return 0; } |

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!