HOME
Preface A Model A Medium A History Variables Functions Control Loops Classes
Source Overview

What Type are You?

“Writing programs is not just getting a machine to perform operations, it is a medium for expressing ideas about methodology.”

Alan Jay Perlis.


3 Key Mecanisms

A computer language is a framework for organising our ideas as processes. In the realm of computation, we are interested in the ‘how’ of things. Implementing an idea implies being able to describe some formal set of rules. To be able to do this we need to learn a standard set of structures - we call these idioms and learning to combine them is a fundamental part of programming.

All computer languages share three principal mechanisms:

1. Means of defining - primitive expressions & primitive data types.
2. Means of combination - compound elements built from simpler (primitive) ones.
3. Means of abstraction - compound elements can be named and manipulated.

All languages come with the ability to perform certain primitive operations. We can perform arithmetic on data, assign values to data, test for conditions and perform simple logic operations. Let us look at a simple example of a primitive expression.

SYNTAX
operands   operators


Eg 1. js.
let x = 10 + 20;

Eg 2. java
int x = 10 + 20;

The above line declares a variable which is assigned a primitive expression made up of two numeral values and a plus sign. In computing language, we call these the operands and the operator respectively. All languages give access to a simple list of primitive operators that enable us to combine operands. In the above example, we combine two numbers but we could equally add two variables if we wanted.

Eg 3. java
float x = 0.15587;
float y = 73.73459;
float sum = x + y;


Data Types

You’ll have noted that when we declare a variable, the name of our variable is prefixed with a special keyword and that these keywords change depending on the language used. In P5js for example, we have used either var or let whereas in Processing we have used int or float. These special keywords denote data types.

Most programming languages come with a limited and relatively small number of data types that are the bricks and mortar for building more complex data structures. Remember, programming is all about data storage and data structures. Specifying the data type determines the values that can be stored and also the type of operations we can perform on that data. So, for example when we write, float dia = 10; in Processing, we are telling the computer that the value of dia is a floating point number and we can therefore store any decimal value. If we declare something like int dia = 10; the keyword int denotes an integer data type and hence we can only store and eventually manipulate whole numbers.

Java/Processing

In contrast, in a language such as JavaScript and P5js, we can use either var or let for both of the above examples. JavaScript is what we call a weakly typed language as opposed to Processing’s strongly typed system. Generally, strongly types languages have stricter typing rules with a set of primitive data types that must be implemented when declaring variables. Theoretically, the means for associating values to symbols implies that the machine must maintain some sort of memory to keep track of the data. We call this memory the environment and more precisely the global environment.

There is another important point that is linked to this idea of memory in programming which is called scope. In general, declaring data within a certain programming structure limits its use to that structure. We call this local scope because we can only retrieve that data from within that structure. In contrast, when we declare outside of any data structure, we can access it from both inside that data structure and outside it too. All variables declared outside of either setup or draw are considered global variables and are accessible from anywhere else in the program.

INPUT

Let us look at a basic example that implements most of the primitive data types in Processing and compare this with the same code but written in P5js.

SYNTAX
data type  


Eg 1. java
int x = 50;
float y = 253.078;
String theWord = "Word";
String infos = "Press B";
char theLetter = 'A';
boolean isDisplay = false;
color myc;

The above code is taken from the Processing sketch, B_variables_02.* These are all global variables and therefore are accessible from anywhere within the program. You can clearly see the different primitive data types for each variable declared. Now let us look at the equivalent code in P5js.


Eg 2. P5js
let x = 50;
let y = 253.078;
let theWord = "Word";
let infos = "Press B";
let theLetter = 'A';
let isDisplay = false;
let myc;

We can clearly see in this example the difference between a strong and weakly typed language. There exist a number of pros and cons for each paradigm but I’ll spare you the details on these. Suffice to say that within our two languages, P5js and Processing, there exists a syntactical difference in how we define variables and the operations we can peform on these too.


OUTPUT

Before we move on to functions, explore the full code for the above sketch and play around with changing values. Compare the differences between the Processing code and the P5js.



Concept 2: Functions...


Notes

Wikipedia contributors. Strong & Weak Typing
Wikipedia, retrieved 15.03.2019