1

I'm learning C and I kinda know how to program on Mathematica.

On Mathematica, I can declare a variable simply by writing:

a=9
a="b"
a=9.5

And it seems that Mathematica understands naturally what kind of variable is this by simply reading and finding some kind of pattern on it. (Int, char, float). I guess Python has the same feature.

While on C, I must say what it is first:

int num;
char ch;
float f;

num=9;
ch='b';
f=9.5;

I'm aware that this extends to other languanges. So my question is: Why would a programming languange need this kind of variable declaration?

References on the topic are going to be very useful.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Red Banana
  • 742
  • 2
  • 7
  • 13

6 Answers6

6

Mathematica, Python, and other "dynamically typed" languages have variables that consist of a value and a type, whereas "statically typed" languages like C have variables that just consist of a value. Not only does this mean that less memory is needed for storing variables, but dynamically typed languages have to set and examine the variable type at runtime to know what type of value the variable contains, whereas with statically typed languages the type, and thus what operations can be/need to be performed on it, are known at compile time. As a result, statically typed languages are considerably faster.

In addition, modern statically typed languages (such as C# and C++11) have type inference, which often makes it unnecessary to mention the type. In some very advanced statically typed languages with type inference like Haskell, large programs can be written without ever specifying a type, providing the efficiency of statically typed languages with the terseness and convenience of dynamically typed languages.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • you statement "consist of a value and a type" is not strictly true for python. variables in python are just names that point (bound) to some object and can be rebound at any time. objects in python have a type. Everything in python is an object. Python is often referred to as dynamically typed and strongly typed. (You can not do "8" + 8) for instance. Weakly typed languages tend to let you do that sort of thing. – Tim Hoffman Aug 30 '12 at 05:13
  • 1
    @huggie I don't know what "strict typing" means. Haskell is statically typed and very strongly typed. – Jim Balter Aug 30 '12 at 05:58
  • @TimHoffman I think the difference between a variable containing a value and a type and a variable being a pointer to a typed value is one of implementation ... Python objects are still type/value pairs, where the value is complex. Both statically and dynamically typed languages can be weakly or strongly typed -- they're orthogonal. C, especially older versions, and Perl are both weakly typed. – Jim Balter Aug 30 '12 at 06:05
  • @jim Balter. Are you sure, where variables are typed (C, java) you can't rebind them to other types, so it goes a bit beyond implementation as the concept of rebinding names and ignoring type is fundamental to the definition of python. I suppose we are splitting hairs here though, given the OP original question. – Tim Hoffman Aug 30 '12 at 08:37
  • @TimHoffman You can't rebind typed variables in C and Java to other types because they are statically typed languages. – Jim Balter Aug 30 '12 at 12:33
3

Declarations are necessary for C to be compiled into an efficient binary. Basically, it's a big part of why C is much faster than Mathematica.

TJD
  • 11,800
  • 1
  • 26
  • 34
3

In contrast to what most other answers seem to suggest, this has little to do with types, which could easily be inferred (let alone efficiency). It is all about unambiguous semantics of scoping.

In a language that allows non-trivial nesting of language constructs it is important to have clear rules about where a variable belongs, and which identifiers refer to the same variable. For that, every variable needs an unambiguous scope that defines where it is visible. Without explicit declarations of variables (whether with or without type annotations) that is not possible in the general case.

Consider a simple function (you can construct similar examples with other forms of nested scope):

function f() {
  i = 0
  while (i < 10) {
    doSomething()
    i = i + 1
  }
}

function g() {
  i = 0
  while (i < 20) {
    f()
    i = i + 1
  }
}

What happens? To tell, you need to know where i will be bound: in the global scope or in the local function scopes? The latter implies that the variables in both functions are completely separate, whereas the former will make them share -- and this particular example loop forever (although the global scope may be what is intended in other examples).

Contrast the above with

function f() {
  var i = 0
  while (i < 10) {
    doSomething()
    i = i + 1
  }
}

function g() {
  var i = 0
  while (i < 20) {
    f()
    i = i + 1
  }
}

vs

var i

function f() {
  i = 0
  while (i < 10) {
    doSomething()
    i = i + 1
  }
}

function g() {
  i = 0
  while (i < 20) {
    f()
    i = i + 1
  }
}

which makes the different possible meanings perfectly clear.

In general, there are no good rules that are able to (1) guess what the programmer really meant, and (2) are sufficiently stable under program extensions or refactorings. It gets nastier the bigger and more complex programs get.

The only way to avoid hairy ambiguities and surprising errors is to require explicit declarations of variables -- which is what all reasonable languages do. (This is language design 101 and has been for 50 years, which, unfortunately, doesn't prevent new generations of language "designers" from repeating the same old mistake over and over again, especially in so-called scripting languages. Until they learn the lesson the hard way and correct the mistake, e.g. JavaScript in ES6.)

Andreas Rossberg
  • 34,518
  • 3
  • 61
  • 72
2

Variable types are necessary for the compiler to be able to verify that correct value types are assigned to a variable. The underlying needs vary from language to language.

vsh
  • 160
  • 4
2

This doesn't have anything to do with types. Consider JavaScript, which has variable declarations without types:

var x = 8; y = 8;

The reason is that JavaScript needs to know whether you are referring to an old name, or creating a new one. The above code would leave any existing xs untouched, but would destroy the old contents of an existing y in a surrounding scope.

For a more extensive example, see this answer.

Community
  • 1
  • 1
Paul Stansifer
  • 1,239
  • 9
  • 10
1

Fundamentally you are comparing two types of languages, ones that are being interpreted by high level virtual machines or interpreters (python, Mathematica ...) and others that are being compiled down to native binaries (C, C++ ...) being executed on a physical machine, obviously if you can define your own virtual machine this gives you amazing flexibility to how dynamic and powerful your language is vs a physical machine which is quite limited, with very limited number of structures and basic instruction set and so on ....

While its true that some languages that are compiled down to virtual machines or interpreted, still require types to be declared (java, C#), this simply done for performance, imagine having to deduce the type at run time or having to use base type for every possible type, this would consume quite a bit of resources not to mention make it quite hard to implement JIT (just in time compiler) that would run some things natively.

Samy Vilar
  • 10,800
  • 2
  • 39
  • 34