Variables are place holders for data a program might use or manipulate. Variables are given names so that we can assign values to them and refer to them later to read the values. Variables typically store values of a given type. Types generally include:
- Integer – to store integer or “whole” numbers
- Real – to store real or fractional numbers (also called float to indicate a floating point number)
- Character – A single character such as a letter of the alphabet or punctuation.
- String – A collection of characters
In order to use a variable within a program, the compiler needs to know in advance the type of data that will be stored in it. For this reason, we declare the variables at the start of the program. Variable declaration consists of giving a new name and a data type for the variable. This is normally done at the very start of the program.
In the following example programs, variables of different types are declared and used in the programs.
Language | Example program |
---|---|
“C” | #include <stdio.h> void main() { int age; float salary; char middle_initial; age = 21; salary = 29521.12; middle_initial = "K"; printf("I am %d years old ", age); printf("I make %8.2f per year " salary); printf("My middle initial is %c ", middle_initial); } |
Pascal | program myexample (output); var age: integer; salary: real; middle_initial: char; begin age := 21; salary := 29521.12; middle_initial := 'K'; writeln('I am ', age, ' years old'); writeln('I make ', salary, ' per year'); writeln('My middle initial is ', middle_initial); end. |
Oracle PL/SQL | CREATE OR REPLACE PROCEDURE myexample AS age NUMBER; salary NUMBER; middle_initial CHAR(1); BEGIN age := 21; salary := 29521.12; middle_initial := 'K'; DBMS_OUTPUT.PUT_LINE('I am ' || age || ' years old'); DBMS_OUTPUT.PUT_LINE('I make ' || TO_CHAR(salary, '99999.99') || ' per year'); DBMS_OUTPUT.PUT_LINE('My middle initial is ' || middle_initial); END; |
Java | class myexample { public static void main (String args []) { int age; double salary; String middle_initial; age = 21; salary = 29521.12; middle_initial = "K"; System.out.println ("I am " + age + " years old "); System.out.println ("I make " + salary + " per year"); System.out.println ("My middle initial is " + middle_initial); } } |
Perl | #!/usr/local/bin/perl $ age = 21; $salary = 29521.12; $middle_initial = "K"; print "I am " . $age . " years old "; print "I make " . $salary . " per year "; print "My middle initial is " . $middle_initial; |
Basic | Dim age AS Integer Dim salary AS Decimal Dim middle_initial As String age = 21 salary = 29521.12 middle_initial = "K" print "I am " & age & " years old " print "I make " & $salary & " per year " print "My middle initial is " & $middle_initial |
In the above examples it is clear that different programming languages have slightly different syntax and data types. However for the most part, variable declaration is straight forward.
Real differences begin to appear when more complex data structures such as arrays and pointers are declared. Such discussion is best left for programming courses.