• # The Programming Primer

General and Gameplay Programming

I have been seeing a lot of confusion with some of our beginning level programmers on the forums lately, namely in what programming language(s) they should be learning, what languages are used for what and how much they should learn. One thing I remember from my days of asking these same questions (many many moons ago) is that there aren't very many if any sources that "humanize" programming from a primer aspect, meaning most learning resources dive right in and kind of expect you to just accept and know some things with very little to no explanation or definition. As such I think a primer like this is long overdue, I'm sure there are others but my hope is to write one here and make it a little more available to a community who seems to be in need of this kind of material. If you are new to programming I would suggest reading over the following sections multiple times and ensure that you really understand it before moving on with your learning. I feel it will help you greatly in the long run.

# Programming Basics

The major portion of this entire document relates to the art of programming itself and won't be particular to any one specific language. You should learn as we go that the language becomes more of a specialized tool to achieve the goal of creating software (that is programming) then being as much of a critical choice. You will start to notice that many times the choice of language really doesn't matter. It is true that some languages perform faster than others at a core level, some languages are easier to use and provide framework built in to them that others do not, some languages you turn into software on your computer and distribute (compiled languages) and others you send out the code and your user's computer will interpret or build it into software (scripting langues). As such we will come back to the discussion on how to choose the best language for your needs at the end of this article. For now try not to focus on a language but learn from the knowledge being provided as programming theory and techniques. So what is programming? Most everyone knows that programming is making software, games, apps, web sites and so on (which technically can all be generalized as software). This is true, but a programmer is actually much more than just the guy that types out commands for a computer to do things, he (or she) becomes a master problem solver. This is the big part that you need to get used to before you can start efficiently programming anything. Know that not a single one of us i(ncluding some friends of mine that code for NASA's rovers) do not know it all! Not knowing everything is fine, it's common. Knowing the fundamentals of what programming is, knowing that your job is to apply that knowledge to solve a problem and having the ability to research, learn and figure out how to do it in the best-performing way for the project at hand is what makes you successful. The point of this section is basically to get you to understand that you will always hit something that you don't know how to do (off the top of your head) and that is normal, what makes you a programmer is the ability to take the challenge in front of you, break it down to the fundamental components and figure out how to arrange those fundamental components in such a way that it efficiently solves the challenge. Never get discouraged when you don't know how to do something, it's just part of our field. Never get upset when something you do doesn't work, learn why it didn't work and find a better way to do it. That is programming.

# Fundamental Components

So now we're starting to get into things. At this point I have mentioned that programming is the art of understanding fundamental components of programming and arranging them in such a way that they efficiently solve the challenge at hand. What is important to know is that these fundamental components exist in all programming languages and that all programming revolves around invoking these things in one way or another. As a programmer in any language you will simply be using that language to use these fundamentals to get the results you are looking for. You will start to notice that when you begin learning a language it will dive right into tutorials that teach you how to use these things but most of them just assume you already understand what they are and why you want them. This is the knowledge I feel is not readily available to new programmers, and although the rest of your programming life will be figuring things out mainly on your own I still think that this is one area that we experienced programmers need to explain better. So as we move on here it is important to realize that all of these things I am about to define and explain exist in every language and are used in every program, game, app or website you will ever create. There fore the better you understand what these are and what they are for the easier it will be for you to actually make software.

## Variables

Variables are something that I consider to be the most important part of programming. A variable is little more than a region of memory (random-access memory in most cases). It's just somewhere that you can store data while your program is running. We use variables every day and in every project that we work on. A variable is "declared" in code, this means that we use a command (slightly based on what language you choose) to tell the compiler "this word" is "this data type". After declaring the variable you can assign values to it, meaning that you can basically say "this word" = "Here's my cool value". Variables take up various sizes of memory based on what "data type" they are, we will get to this next but for now you want to really understand that a variable is a term used to define a word in your code that is used to refer to a piece of memory that stores a value. Just like in algebra how letters are used as variables in an equation, in fact almost every time you need to calculate anything in your code you will use a programming variable in an algebraic equation almost exactly like you did back in school! Example: (This is C++ just to illustrate real usage)  int x; x = 10 + 2;  In the example above we see how to declare a variable (int x;) which is telling our C++ compiler (the program that turns our code into software) that we want x to mean an integer value (we will discuss that a bit more in just a bit) then we are telling it to assign the value of 10 + 2 to that variable. Now we have 12 stored in memory and we can call on that later! Simple concept but it is one of if not the most important thing you will do every day.

## Data Types

Data types is a term used to define what type of variable memory we are using. Some languages will mask this from you and give you a more global var type that means (any kind of memory) while others are what we call "strongly typed languages" that require you be specific about what type of variable memory you want to use. In either case it is very important to understand what data types are, even when using a "soft typed language" that doesn't require you to specifically declare what type of variable memory you want. Understanding data types, what they mean and how they work under the hood makes your life easier in the long run and leads to less unknown behavior. There are 3 logical classifications of variable data types - that is to say in your brain there are 3 different types of variables you will use. Numbers, letters and booleans (true / false). From here there are actually numerous different sub classifications and even some extended types that won't really make sense until later on, but for now I recommend getting familiar with the basic 3 and their sub classifications. Lets look into these a bit more... Numbers You will use numbers all the time when you are programming. There are many different types of number variable data types that you can use and which one will depend on what you need at the time. Sometimes you may want to consider ram space requirements for these different types but honestly this rarely matters. Modern computers have tons of ram, unless you are working on something that is very performance oriented and requires tons and tons of variable data space you really won't need to pay too much attention to this. However it is always better to use the least amount of RAM that can hold the values you need. Information on data types and memory size usages is abundant and I won't reiterate this same information here, you should look this information up and learn it on your own after this (consider it your first learning on your own assignment, you'll be doing that a lot anyway). int An "int" is an integer value. It holds a whole number, can be "signed" or "unsigned" (meaning can support negative "signed" or positive only "unsigned"). Use this when you need to work with small to medium whole numbers. long A "long" is a "long integer". Depending on the computer it may support up to twice the size of the number as an integer (some computers make integers and longs the same size). Just like an integer this can be "signed" or "unsigned". It is a whole number, you use this when you need to ensure you have space for very very large numbers. In most cases integer values will suffice. float A "float" is a "floating point integer" which means it's a decimal point value. Just like integers and longs this can be a "signed" or "unsigned" value. Use these when you need to store a numeric value with a decimal point. Note that most languages require that you place the letter f at the end of a float's value. example:  float x = 1.5f;  double A "double" is a "double precision floating point integer". Much like you probably can guess this is a decimal pointed value that can store twice as large of a number as a floating point integer. You use this value when you need to ensure that you have space for an extremely large decimal pointed value. Double's do not require the trailing letter like a float does. Characters Characters (known as "char") are letters, many languages have moved beyond this particular variable data type in favor of more useful "string" styles of data types which are a bit more complicated to understand but knowing the character data type is still fairly important latent knowledge in my opinion. A character is one (or two bytes) of RAM that can store a letter. I say two bytes because some non American English languages use what we call a "wide character" Wide characters require more data to define the character than a normal American English character does. Wide characters can also be used for American English characters as well under different encodings. The more you program and the more you learn the more this stuff will make sense. For a primer the important part to know is that a "character" is normally one byte in memory that stores a letter. A string is multiple characters that can comprise a word, a sentence or even an entire article. Little more homework from here: research characters, encoding and strings. Boolean Boolean (or more commonly "bool") is a very small (1 bit) memory allocation that is used to store the very simple true or false value. If you remember your binary mathematics course (assuming your school made you learn it like our's did) binary is little more than 0 (false) or 1 (true). This is really at the heart of everything that any computing device does, little more than high speed timed transmissions of 0's and 1's that flip transistor values and when rigged together and activated within the proper sequence can cause various outcomes. (Ok, too much there, but point is yes and no, true and false, 0 and 1 are very important). However at the level of programming that you are most likely to do, assuming that you're not building device drivers in assembly your use of bool data types will simple be a means of storing a yes or no that you can later check and act upon. Examples being something like isAlive, or hasWeapon or what have you. These are also commonly referred to as "flags".

# Stop and make sure you understand!

## Functions and Methods

Functions and Methods are two terms that are interchangeably used to refer to a written portion of code that performs a task and optionally returns a value. In my opinion it is not correct to interchange these words as they where initially described to me to mean two similar but different things that we will address as we go on here. It is important to note that I am in the minority that believes there is a strict difference in these terms, as such you can and should assume that whenever you see either of these words they could mean either of the two definitions. This is a debatable theory of sorts where there is no official answer that dictates who is right and who is wrong, you can agree with the way I think of it or not and it will not directly effect your knowledge or abilities. Functions A function as mentioned is a bit of written code that performs an operation and optionally returns a value. When I say function I specifically am speaking of what we call procedural coding. That means that a function is just a function, it can be used by itself and it is not part of a class or data object (that we will cover soon). The way you write a function can vary from language to language but the core fundamental that you are doing is always the same. You are assigning a word that you can use in your code that can perform an operation and optionally return a value (yes I'm a broken record). Functions can take what we call "arguments", an argument is a variable that you give to the function, this is something the function can use in its operation that helps to determine the value that it might be returning. In strongly-typed languages the return data type and argument data types are very important, in some other softly-typed languages the return types and data types may not matter as much or may not even be required at all but it does always help when you know in your mind what type of data you are feeding in and what type of data you are expecting to come out. This might be best shown with another small example, again I will be using C++ here, remember that how you write this in another language might look slightly different but the technique is the same and what it does is the same.  int startNumber = 5; int otherNumber = 3; int addNumbers(int firstNumber, int secondNumber) { int resultNumber = firstNumber + secondNumber; return resultNumber; } int myNumber = addNumbers(startNumber, otherNumber);  What we see here is that we start off declaring (and defining) two integer variables that we will be using. startNumber and otherNumber, we set the values 5 and 3 to these variables. Then we define a function, we say it will return an integer, and that it takes two integers as arguments, (firstNumber and secondNumber). It is important to note here that firstNumber and secondNumber only exist within that function and nowhere else. That means that outside of the function's { } braces firstNumber and secondNumber may cause an error (in a strongly-typed language) or will always be 0 (in a softly-typed language). Likewise startNumber and otherNumber normally do not exist within the function itself (this is "scoping" which is a bit more advanced theory you will learn on your own later). Inside the function we declare a new variable (that only exists within the function) and assign it the value of firstNumber + secondNumber. This makes resultNumber 8 (duh). We then "return" this resultNumber. After this we actually use the function to make something happen. We create another new integer variable called myNumber and we assign it the value returned from the addNumbers function, we give the addNumbers function the arguments of startNumber and otherNumber (which inside our function turn into firstNumber and secondNumber respectively). What happens here is that at the end of this example myNumber is 8. Although this example is very pointless it simply demonstrates how you could create a function to do some work and return a value. Now in your code instead of writing that work out every time you can just use that function to get the result you want. You use functions often to keep yourself from typing out the same "work" over and over again. It is always good practice to put "work" into functions whenever possible, in the long run it will limit the time you spending typing code and make it easier to make changes to everything that uses that function all from one spot instead of searching over your entire code and changing it everywhere you wrote that same "work". Methods Again, please note that many other programmers will say that a method is exactly the same thing as the "function" we just defined and technically they are right (as there is no answer to which term specifically means what). Unfortunately I kind of have to jump ahead of the next section and mention this now, I'm sure it's a bit confusing but it has to be said here to limit the confusion if you are reading somewhere else and you see people talking about "methods" meaning the same thing as I just defined for functions. Just always remember that depending on who says either of these words will make the difference on what that word means to them. When I say "Method" I am referring to a function that is a part of a class or object as we will cover next. Many other people say "method" and what they mean is exactly what I just defined as a function. Vice verse some people may use the term "class function" or "class method" to mean exactly what I mean when I use the word "method". To a beginner this is a bit confusing, the easiest way to deal with this is just to remember that both words can be used interchangeably and no matter how you look at it they both refer to a portion of code that performs an operation and optionally returns a value. Sometimes they might be "procedural" like we just saw in the function example. Sometimes they might be part of a "class object", but either way what they do is exactly the same, they do work, might let you provide arguments and might return a value. If and when you start working with another coder it is good practice to discuss this and agree upon what you will mean when you use these words to help prevent confusion as you go on.

# Where to go from here?

Report Article

## User Feedback

You need to be a member in order to leave a review

## Create an account

Register a new account