New To Software Development?

In this post I explain the basics of computer programming. If you are completely new to software development, this post should serve as a useful guide. The following diagram illustrates a basic program. Essentially, a program consists of one or more inputs and generates one or more outputs.

In This Post


Top

What Is A Program?

A program is basically a set of instructions that transforms a given set of inputs to one or more outputs. Program inputs include key presses, local data (say from a file), a database and so on. Program outputs include screen display, file system, memory, or a database. For some programs, configuration data may be required.

The key point to take from this is that any computer program takes inputs and generates outputs.

Top

Program Basics

All programming languages share common constructs. The syntax (i.e. the characters required) may differ, but the semantics (i.e. the meaning of a given construct) remain consistent.

Variables

Variables are a fundamental aspect of most programming languages. Variables simply allow the storage of a value. The value could be an integer, a real number (i.e. fractional), a string, a character, and so on. The syntax does vary across languages, but the end result is the same.

The following are some examples of variable declarations.


// Declare a variable, ch, that holds a character value of 'a'.
char ch = 'a';

// Declare a variable, x, that holds an integer value of 20.
int x = 20;

// Declare a variable, y, that holds a float value value of 1.5.
float y = 1.5;

// Declare a variable, s, that holds a string, hello world.
// One can thing of a string as a list of characters.
string helloWorldString = "Hello World!"; 

Top

Conditionals / flow control

A conditional is essentially an if statement. So, one might say, if a given age is greater than 30 then do something, else, do something else. In code, this might look like the following...

int age = 30;
if (age > 30)
  ProcessAgeGreaterThan30(age);
else
  ProcessAgeLessThanOrEqualTo30(age);

In the above example, I assume ProcessAgeGreaterThan30 and ProcessAgeLessThanOrEqualTo30 are functions defined elsewhere.

Top

Functions

In mathematics, a function takes one of more inputs and returns a value based upon the supplied inputs. A simple example is an add function that might, say, add two to the supplied value. We might write this function (mathematically) as...


f(x) => x + 2

In most programming languages, the function requires a name. Examples functions to add two to an input follow...

// A JavaScript function
function Add(x)
{
  return x + 2;
}

// A C function
Notice how the return type, int must be specified.
int Add(x)
{
  return x + 2;
}

// A C# function.
// C# requires all functions are declared within a class, boring I know!
// The static keyword simply states that the function is global.
// Anyone may use the function, MyFunctions.Add(2), for example.
class MyFunctions
{
  public static int Add(int x) =>
    x + 2;
}

The above may look daunting to those starting out in Software development. All of the above are simply mechanisms that state the following... I have code that I need to be executed. This code is used in many places, so, rather than copy and past, I give this code a name (function name)

Unfortunately, the syntax required varies across different languages.

Functions should always only use supplied vaues and return a result based upon these value. That is, a function should be determinsitic. For example, the Add function, mentioned earlier, always adds two to the supplied input value. The result is always the same (deterministic) assuming the same input values.

Top

What Is A Program - Redefined

Looking at the above you might be tempted to think that a program consists of only variables and functions. Spot on, that's it, essentially.

Problems are broken down into functions. The core application, that takes input and generates a given output will use functions to achieve its goal.

No comments:

Post a Comment