Variables, Functions, and Conditionals Made Easy

Programming is powerful. It gets its power from variables, functions, and conditionals. A variable is storage for dynamic information. A function is one or more instructions that perform a specific task. A conditional expression decides what should be done in the event that specific information meets particular criteria such as being true or being a particular number.

A variable is often likened to a container in which information is stored and from which it is retrieved. As its name implies, that information is variable; that is, it can change as the program runs. In a video game a character’s health is often represented by a number from 0-100. This health number would most likely be stored as an integer variable named, you guessed it, “health.” The value of “health” will change throughout the game depending on, among other things, how many shovels the character is hit with.

A function is comprised of one or more instructions. It’s a re-usable mini-program of sorts. A function’s instructions might involve subtracting the number 20 from a character’s “health” variable every time it got hit with a shovel. More likely, instead of 20, the function would subtract the value of the variable “damage” from the variable “health.” That way the variable “damage” could be updated according to the kind of shovel the player is hit with. A coal shovel would do more damage than a snow shovel which, in turn, would do more damage than one of those little plastic sand-castle shovels

A conditional determines what will happen as the result of new information. For example, a conditional might continuously check to see what a player’s “health” is, and, if it’s 0 (or less), decide to play a death animation and end the game. Otherwise, it would continue to check until and unless the character meets its grizzly demise at the hands of a shovel-wielding maniac. Video games are pretty violent these days…

Functions and conditionals are and do whatever the programmer tells them to be and do. Functions can be custom-made or sourced ready-made from libraries. Variables, however, come in specific types. Here are some important variable types;

  • Integer: use this type when dealing with whole numbers

  • Float: use this type when dealing with numbers that have decimal places

  • Vectors 2 and 3: use these types when dealing with two or three numbers at a time, for example, when dealing with 2D and 3D coordinates

  • Boolean: use this when storing a true or false value

  • String: use this when you want to store text (note that “15” stored as a string is the word 15 spelled with numbers and not the number 15)

There are other variables as well, some of which are specific to certain programming environments, but learning these is a good start. Understanding the concepts behind variables, functions, and conditionals is the foundation for a strong programming education!

Leave Comment

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