You are on page 1of 6

Programming Summary

A program is defined as any file or set that instructs a computer operating system or processor
to act on a series of commands. All programs execute from left to right, top to bottom, much like
how the English language is read. In addition, all programs execute on a single process or
thread, however some programs may be multi-threaded.

All programs have a point of entry and exit. This is generally known as the main function. When
a program runs, it begins at the top of the main function, executes all code within, and then exits
the program with an exit code. An exit code notifies the operating system on the state of the
program when it closed. Did it crash, or was the program closed normally? The exit code
determines the state of the program, and then tells the operating system, which may then
optionally alert the user if the program crashed or not.

Despite there being many different programming languages, all with different intentions and
syntax, all programming languages share the same logic; it is like saying, if one programming
language is learned, then they are all learned. The only significant differentiating factor is the
syntax.

Types
Types are very important to computers, and all programming languages treat them differently. A
type is an allocation of memory that references a specific kind of data. Examine the following:

123456
abcdef
A typical type differential is between numbers and letters. Numbers are used to form
calculations and to count information, while letters are, generally, used within a language to
define words and grammar. They have two different functions and it is important to note the
difference between them when programming.

For example:

1+2=3

a+b=?
The technical reason is because numbers, or integers, typically use 4 bytes of memory, while
letters only use 1. This is a very involved topic in its own right, but know for now that there are
different forms of data known as types.

Variables
Variables are containers for data that can be accessed from a specific reference. Variables are
all treated differently between languages and there is a major key difference between variable
style. For the purposes of this document, I’ll refer to C style and JavaScript style. C style uses
static typing and JavaScript style uses dynamic typing. In statically typed languages, there is
strong type security and variables all must be declared with their appropriate type. The latter of
the two does not have the same security, and the typing does not have to be immediately
declared. Examine the following:

C Style
int x = 5;

JavaScript Style
var x = 5;

Our variable declarations tell our program what type of variable we are working with. In C Style
and other statically typed languages, the type must be explicitly declared. We are telling our
program that we want to create a variable x, a type of integer with a defined value of 5.

In JavaScript style, the program doesn’t necessarily care about the type, only the name. When
we create a variable, it is a process known as declaration. When a value is set to a variable, this
process is known as definition. In both examples we can say, we are declaring a variable, with
the name x, and defining a value of 5. All variables need to be named, although they don’t
necessarily need to be defined at creation time.

Variable names can be most anything, however, most languages have a reservation on certain
keywords. int int; or var var; would not be correct syntax. The above example can be rewritten
as:

C Style
int our_number = 5;

JavaScript Style
var ourNumber = 5;
, for instance. In statically typed languages, we cannot redeclare or redefine our variable as a
different type. It will exist as its first declared type throughout the lifetime of the program or
scope. In addition, you cannot have two variables with the same name within the same scope.

C Style
int our_number = 5;
char our_number = ‘a’; // Redeclaration error

JavaScript Style
var ourNumber = 5;
var ourNumber = ‘a’; // Redeclaration error

A brief note: in statically typed languages, you cannot assign a value to a variable that is not a
respective type. Examine below:

C Style
int our_number = 5;
our_number = ‘a’; // Type error; our_number declared of type int

This is not the case in dynamic languages:

JavaScript Style
var ourNumber = 5;
ourNumber = ‘a’;

Once a variable is created, it can be used for any appropriate purpose during its lifecycle.
Examine the following:

C Style
int x = 5;
int y = 5;
int z; // ‘Null’ declaration 1
x = 6;
y = 2;
z = x + y; // z value will be 8

1
A null variable has no value. Null variables do not equal 0; they contain no value information. Despite
being the truth in all languages, not all languages evaluate null the same. In C Style, 0 and null are not
equal, but in JavaScript style, 0 and null are equal. If you add +1 to 0, the result will be 1, but +1 to a null
value will result in an error.
Types (continued)
As mentioned, there are a variety of different types in computing. Perhaps most ubiquitous of all
is the integer type. An integer is a representation of a number; of what variety is dependant. For
example, -124 and 4,344,448 are both considered integers, but are not considered equal. A
positive integer can either be signed or unsigned.

Arrays here

Statements here

Functions
Functions are areas of code that can be reused in a program. Functions are created on the
stack2 and variables within only survive as long as the scope of the function; once a function has
completed the variables will be destroyed. In other words, a variable cannot be accessed
outside of the end of a function. Examine:

C Style
int add(int,int);
int add(int x, int y) {
return x + y;
}

int my_num = add(6,2); // my_num will be 8 here

JavaScript Style
function add(x, y) {
return x + y;
}

var my_num = add(2, 4); // my_num will be 6 here

Functions, like variables, must also have typing. In dynamic languages, there is usually only one
keyword for functions, and the type doesn’t need to be declared. Functions that have return
statements return a value specified in the statement. In the above examples, we are returning

2
The heap and stack are very complicated programming constructs. Just know for now that when a
variable is created, it is created in a space known as the heap or stack.
the value of x + y, introduced as parameters or arguments. A parameter initializes a variable that
will be used within the scope of a function. Above, x and y are declared as function parameters
only to be used within the function add. Arguments are variables passed to the function that
contain data. The above could be rewritten as such:
JavaScript Style
function add(x, y) {
return x + y;
}

var number_to_add_1 = 10;


var number_to_add_2 = 15;
var result = add(number_to_add_1, number_to_add_2); // result will be 25 here

Functions can call other functions from within itself, and more advanced topics cover subjects
like recursion and memory allocation when dealing with functions.

You might also like