Week 2: Arrays and Functions

Hi,

Sorry for this late post. It is a week late, but on the other hand. This weekend there will be two posts!

So.. A new week of Game Programming. This week we have gone through functions, arrays and pointers. This week have been more confusing and hard than the first week. I feel that the reason for that is that I didn’t give it much thought, or much time. The second week I didn’t have so many hours of studying as I should have had, which I later on understood.

What are pointers? These are variables that hold memory addresses to a variable of the same data type. I will show you the syntax in the example under the explanation of what a function is.

What is a function? They are statements which can take zero, one or more parameters. In every program there are at least one function, and that is the function main().
Here is an example of a program with multiple functions:


#include 

void Swap(float* a, float* b)
{
float SwapHolder = *a;
*a = *b;
*b = SwapHolder;
}
int main(int argc, char* argv[])
{
float Num1 = 500, Num2 = 499;
Swap(&Num1, &Num2);

std::cout << "Number1: " << Num1 << " Number2: " << Num2 << std::endl;

return 0;
}

1st line of the code: I include a standard library. This specific library includes the functions so I can use std::cout.

Line 3-8: Here I am declaring my function. This is called Swap and it is of the data type void. The only thing this function does is to Swap 2 numbers with each other. As you can see on Line 3 it says void Swap(float* a, float* b). This is a function with two parameters, which is declared as a and b with the data type float. The * after float is a syntax for creating a pointer. As said, a pointer will refer to a variables (with the same data type) memory address. If I then write the ‘&’-symbol before the variable that the pointer is equal to, then it will be de-referenced and will show the value that the pointer is pointing at.

I know, this is extremely confusing. I will probably get this soon enough. This also demands extremely much time! Which I have been giving it.

Line 11, 12 & 14: At line 11 I declare two variables Num1 and Num2 of the datatype float. I initialize the value 500 to Num1 and 499 to Num2. After this I call the function I made before, Swap, and within the parentheses I write the parameters which are connected to the variabels I made in the line before. I have an ‘&’-symbol before the parameters. This is a pointer thing, which I will talk about soon.
Line 14 just write the result in the log. It will say “Number1: 499 Number2: 500″, because of the Swap-function.

What are arrays? Arrays are structures with a collection of elements of the same data type. Arrays are pretty easy. I will show you an example and then explain it:


#include 

int main(int argc, char* argv[])
{
int myArray[5] = { 515, 2, 3, 4, 5 };

for (int i = 0; i < 5; i++)
{
std::cout << "myArray: [" << i << "] Value: " << myArray[i] << std::endl;
}

std::cin.get();
return 0;
}

In the int main function I have written: int myArray[5] = {515, 2, 3, 4, 5];. I have declared an array with 5 elements. The elements inside a array is numbered like: 0, 1, 2, 3, 4. So the 5th element is the 4th value in the array. So if I want to take out the value 5 from myArray I will have to write:
myArray[4];. This will be equal “5”.

I hope you have learned something new, at least I have!

Cya all

/ Alex

About Alex Henningsson

2014  Programming