Comparison of Programming Languages (basic Instructions) - Variable and Constant Declarations

Variable and Constant Declarations

variable constant type synonym
ALGOL 68 modename name «:= initial_value»; modename name = value; mode synonym = modename;
C (C99) type name «= initial_value»; enum{ name = value }; typedef type synonym;
Objective-C
C++ const type name = value;
C# type name «= initial_value»; or
var name = value;
const type name = value; or
readonly type name = value;
using synonym = type;
D type name «= initial_value»; or
auto name = value;
const type name = value; or
immutable type name = value;
alias type synonym;
Java type name «= initial_value»; final type name = value;
JavaScript var name «= initial_value»; const name = value;
Go var name type «= initial_value» or
name := initial_value
const name «type» = initial_value type synonym type
Common Lisp (defparameter name initial_value) or
(defvar name initial_value) or
(setf (symbol-value 'symbol) initial_value)
(defconstant name value) (deftype synonym 'type)
Scheme (define name initial_value)
ISLISP (defglobal name initial_value) or
(defdynamic name initial_value)
(defconstant name value)
Pascal name: type «= initial_value» name = value synonym = type
Visual Basic Dim name As type Const name As type = value
Visual Basic .NET Dim name As type«= initial_value» Imports synonym = type
Python name = initial_value synonym = type
S-Lang name = initial_value; typedef struct {...} typename
Fortran type name type, PARAMETER :: name = value
PHP $name = initial_value; define("name", value);
const name = value (5.3+)
Perl «my» $name «= initial_value»; use constant name => value;
Perl 6 «my «type»» $name «= initial_value»; «my «type»» constant name = value; ::synonym ::= type
Ruby name = initial_value Name = value synonym = type
Windows PowerShell «» $name = initial_value
OCaml let name «: type ref» = ref value let name «: type» = value type synonym = type
F# let mutable name «: type» = value
Standard ML val name «: type ref» = ref value val name «: type» = value
Haskell «name::type;» name = value type Synonym = type
Forth VARIABLE name (in some systems use value VARIABLE name instead) value CONSTANT name

^a Pascal has declaration blocks. See Comparison of programming languages (basic instructions)#Functions.
^b Types are just regular objects, so you can just assign them.
^c In Perl, the "my" keyword scopes the variable into the block.
^d Technically, this does not declare name to be a mutable variable—in ML, all names can only be bound once; rather, it declares name to point to a "reference" data structure, which is a simple mutable cell. The data structure can then be read and written to using the ! and := operators, respectively.

Read more about this topic:  Comparison Of Programming Languages (basic Instructions)

Famous quotes containing the words variable, constant and/or declarations:

    Walked forth to ease my pain
    Along the shore of silver streaming Thames,
    Whose rutty bank, the which his river hems,
    Was painted all with variable flowers,
    Edmund Spenser (1552?–1599)

    We can never establish with certainty what part of our relations with others is the result of our emotions—love, antipathy, charity, or malice—and what part is predetermined by the constant power play among individuals.
    Milan Kundera (b. 1929)

    I judge a man by his actions with men, much more than by his declarations Godwards—When I find him to be envious, carping, spiteful, hating the successes of others, and complaining that the world has never done enough for him, I am apt to doubt whether his humility before God will atone for his want of manliness.
    Anthony Trollope (1815–1882)