Jump to content

  • Log In with Google      Sign In   
  • Create Account


Like
20Likes
Dislike

The Programming Primer

By Dan Mayor | Published Aug 06 2013 03:54 PM in General Programming
Peer Reviewed by (Bluefirehawk, Josh Vega, Dwarf King)

programming function method class data type variable

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!


At this point we have covered the basic variables and data types and as we continue basic understanding of variables and data types is expected. Be sure that you truly understand everything above before you read on, if you have questions or find that I did not adequately explain something then by all means start flexing your programming muscle and hit the web in search of more information on whatever you may not be absolutely clear on. This will become a golden ability the more you get into programming, not only understanding what I just explained but the ability to go that extra step and make sure you understand it by finding more documentation and resources that further your understanding of both what I have introduced and what I have left out. Keep in mind I did leave a lot out, what I left out is no less important but it is normally more complicated. I am leaving it up to you to expand your knowledge and find what I left out on your own because this is training for your later programming. You now have a basic knowledge and some key phrases that will set you down the right path, simple searches using these terms will unlock a wealth of information and your willingness to read until you can't read any more is what will make you a better programmer down the road. (To other experienced coders, feel free to point out what I have left out but also understand I'm doing it on purpose to teach some beginners that ever so important art of studying & learning on their own).

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.

Classes and Objects


Classes and Objects are also a bit confusing as they are two terms that are supposed to mean slightly different things. Some will argue the terms are interchangeable while others (like myself) insist that they mean specific things. I believe I am in the majority in my beliefs in this one being that they mean different things, however always be aware that something you're reading might use one or the other of these terms to mean the same thing (just like some say method when I would say function, some may say object when I would say class). Again conforming or not conforming to the more popular belief of what each of these words mean won't directly effect your skills or abilities as a programmer.

With that said, a class is a very special data type of sorts. It is a region of memory that can hold a collection of variables and functions within it. The variables and functions within a class are normally if not always related in some way, for example a WeaponClass might have variables that tell the weapon's size, its strength, durability and might have functions that you would use to get these values such as maybe an attack function that would return the amount of damage the weapon deals based on its current durability and then reduce the durability slightly to simulate the weapon degrading over time. Hopefully why a class would be useful just lit up in in your mind (oh That's why I want to use them!). A class is a means of declaring a "thing" that has multiple parts, being variables and functions that can do work on the variables of that class (and or on arguments or other variables that the class can see, again this gets into scoping that you will be on your own to go learn about).

So now I will put my foot down so to speak and explain why I consider functions, methods, variables, members and objects all different words that mean different things. Keep in mind this is my way of thinking, some will agree some won't and at the end of the day it's a fruitless argument, they are just words. When I say that a function is not a method that's because to me the word function means a procedural portion of work. Something that exists by itself and is not inside a class, to me this makes it easier to immediately know what my team is talking about when we say "This function is doing this". A method is a function that exists within a class, although it does the same thing, that is it optionally take arguments, it does some work and optionally returns a value the difference to me is that a "method" resides within a class no matter what. This can and will be argued by others, make your own decision on what you think each should be called just know that there are two slightly different things at play here, one that exists by itself and one that exists within a class.

What I just started mentioning in this section is "members". This was something that seems to be pretty much agreed upon as a term that means a variable that exists only within a class. That is exactly how I mean this term and most of the time when you see someone say "member" in programming this is what they mean too. In some rare occasions you will see some people say "member" when they just mean a variable that exists by itself but these are rare instances. Be aware that member might be used to refer to a variable, also know that there are times when people will refer to a member by calling it a "class variable" or a "class property". Again it's pretty much all potato pototo and they are just words that are referring to sections of memory that you can assign and get values from.

Now, classes themselves are a bit tricky to learn at first as they are more strict about declaration, definition and use. Some languages (mainly C++) require there be a very distinct separation between the declaration (where you say this is my class, these are its members and methods) and the definition (where you actually write the methods). Most other higher-level languages don't enforce (or even support in some cases) this distinction. In these other languages you write your class once all together, this means that you write it just like you are declaring it but when you hit a method you go ahead and define your method (write the work) right there inside the declaration. This is something you will need to learn to do based on the language that you are coding on, it's mainly important to understand that a class is just a collection of members and methods that are somehow logically related to you as the programmer. They are used to make your life easier.

To use a class you have to create an instance of that class. This means that you instantiate a name to be an area of memory that contains the members and methods of a class, much like you would create a variable. So this means that your class that you write is more like a blueprint, it is nothing but a definition until you create an instance of it (or as most of us would say, an object). There are some more advanced topics about classes that include singleton and static classes that don't require this instantiation but that is another one of those things you will need to go learn on your own. (Yes I'm making you work for it a bit, you will need to work for your answers ever day as a programmer and you might as well get started now).

Now we come to where I make a big difference about what "class" means and what "object" means. To me (and I do think this is the more popular understanding) when I say "class" I mean the declaration and the definition of a class. The code that makes the blueprint so to speak. When I say object I am referring to an instance of that class (the actual usable name that represents the memory that contains members and methods as defined by the class). Some people will still use the word object to mean what I explain as being a class, some people don't use the term object at all, be aware that either word can mean what I have referred to as "class" depending on who is saying it. In some cases where people don't make this distinction in their own terminology they will normally say "class instance" or "object instance" to refer to what I call an object. When using the words "class instance" or "object instance" it's pretty cut and dry and not open for much debate, this will always mean what I mean when I say object. Adding the word instance makes the term no longer interchangeable with the word "class" as it is referring specifically to a word you use in code that means the area of memory that holds an instance of the members and methods that you have defined in your class.

Wait I'm confused as hell!


Good, and as well you should be. Programming is not easy, it's not simple, and it can not be completely explained or understood from a single article that spans a few pages. What I have set out to do in this entry is to arm you with some theory and terms that will lead you down the path of glory. I have purposely left some of the more complex things out, I have purposely written this article in a way that should have you asking questions and at this point I want to set you free. Not to throw you out to the wolves but to get you started with the researching and studying that will become an invaluable skill to you as you continue through your programming career. I don't mean that you should walk away from this article baffled, perplexed and feeling like it was a worthless read. I want you to walk away from this article understanding everything I have presented in it, and to do that I want you to head out and do some research to answer the questions I have given rise to in your head. I don't mean to leave you stranded and you can of course contact me if you'd like, my response will be that I will go to a search engine to find resources that further explain what you are having problems with and I will tell you what search phrase I used and give you links to the resources to go read. This is not me avoiding answering you but again I can't stress this enough you need to figure it out. This is what will make you or break you as a programmer. Yes there is always someone out there with the answer, and yes you can take their answer, apply it and make it work but not understanding why their answer works will just lead to more problems down the road. This little idea of researching, reading and learning why things work the way they do will empower you to no end and it's something that you will see all experienced coders trying to force you in to. As a beginner myself it always aggravated me that I asked a simple question that needed a little 10 words or less answer and people always referred me to huge articles that didn't seem to answer the questions (but when I read it I came to the answer myself). This is why we all send you to read way more than you wanted to know, it teaches you to answer it yourself and arms you with behind the scenes information that will answer more questions later on down the road.

Where to go from here?


At this point I assume you have heeded the warning that I issued many times in this article and you have gone out and researched, answered your questions and that you completely understand everything I have presented to you in this article. This will have undoubtedly led you to even more knowledge than what I have presented here (such as static and singleton classes, data structures, pointers, arrays and so on). Some of you will have read over this and will understand everything I have said without having to do external research, that is good but is not particularly better than someone who got all confused and had to go look up more information on what I said. Actually they might even be in a better position than you are right now because they actually got equipped with all that which I left out when they went looking for more information. In either case, don't worry, this is a primer that is meant to give you a knowledge of what programming is all about behind the scenes and to give you theoretical advice of sorts on what to go learn about and what it's used for in the long run.

So from here, go be free, be a programmer! I know it sounds generic but that's the best I can offer to you as a beginner at square one. Learn that which I have presented and learn it well, recite it in your sleep! Don't worry so much about how you will implement these fundamentals in any one particular language but understand what they are. Once you understand all of this you are equipped to understand what tutorials are saying and you are now able to go learn whatever language you want! How to pick which language you want is a completely different discussion that warrants its own entire article that I will try to provide in the near future. Till then, I'm sorry to leave you hanging but the short idea of what this article will talk about is "what does the language do?" "how common is the language?" "how many resources are out there that I can understand to learn from?" and "how efficiently can I write code in this language?".

I hope at this point that I have rambled on enough and instilled the importance of learning to learn being the most important fundamental of programming. I'm hoping that I have exposed and made a little sense of the core tools, theories and features that are used to make things happen and given you an idea of how you might think of these things working together to make something happen. Even though you still may not actually know how to do it you should be able to think of things like....

"I can use some variables to hold statistics for a character. I can arrange these variables as class members and create a class that represents a character in a game. I can then write methods inside this class that will calculate leveling up and other complex equations that would pertain to a character in a game."

So forth and so on. That is what programming is all about ladies and gentlemen. Taking an idea and breaking it down to fundamental components or theories then researching a language that gives you the means of making that happen and studying the techniques of that language that allow you to do what you want. When you don't fully understand something don't get discouraged, get educated! Remember it's all pretty simple when it all comes down to it, what you are learning is just the subtle nuances of how to make these things happen in an order that works for what you are trying to do using the language as your tools. And finally, with all of that I hope I have clarified what programming is and what you want to do to learn how to do it. I hope I have given you terms that you can research and study to learn more and given you the general idea of how you make solutions to problems using the fundamentals that these terms refer to.



About the Author(s)


Dan Mayor is a professional programmer and website designer with over 15 years of experience, working mainly in the freelance and private contractor areas. Currently he is shifting focus to more custom projects, software and game sales.

License


GDOL (Gamedev.net Open License)




Comments
Nice article for starters to dive in

nicely written

Nice article.

l like this part.

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.

Take the challenge and find the solves,really cool.

Thanks to everyone for your comments and votes, I'm glad to see that this article seems to be a beneficial read for many people.  In the not so distant future I will write up some additional theoretical articles on some more advanced topics such as networking, threading and such.

As a beginner, I feel I would loose it at the example of adding numbers.

 

It just does too much demonstration instead of actually doing something. Imagine a beginner who will finally figure out that the best way to write that piece of code in practice would be:

 

int myNumber = 8;

 

5 + 3 is 8, duh! :)

 

I would mark this as peer reviewed, because honestly this article does what it claims to do: confuse the hell out of beginners :). It basically throws to the face all these concepts and says "Get your brain familiar with these words!". It is not for everyone.

Good article. The adding numbers example is standard for a function example. Every beginner C++ book I own and have read through at book stores usually have 

int add(int, int);
int subtract(int, int);
int multiply(int, int);
int divide(int, int);

Concerns me if a beginner is offended by a very standard and very common example for functions.

 

Great read and straight to the point.


Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.




PARTNERS