13

In older versions of Fortran, e.g. 77, variables did not need to be declared. If the name starts with I, J, K, L, M, or N then it would be assumed to be an INTEGER. Otherwise, it would be assumed to be REAL (float).

This might still be true of more recent versions but I have no experience of them and this is a retro group so I am not asking about them.

My question is on the origin of the convention. I have heard one explanation that I and N are the first two letters of INTEGER.

However, similar habits are common in maths. i, j, k, l, m, and n are very popular for integers whereas as x, y, and z are very popular for real numbers. Which influenced the other? Did existing maths habits influence Fortran or did Fortran influence maths?

5
  • Re. 'recent' versions (90/95/03/08/15): The default implicit types for variables have not changed. They can be adjusted using the implicit statement, or switched off entirely with implicit none. I suspect most Fortran users put implicit none at the top of every program unit, since implicit typing doesn't make much sense unless programs are entered on punch cards! Jun 8, 2018 at 12:34
  • @IanThompson Thanks. I think that IMPLICIT has been in the language for a long time. It sounds familiar. A quick search suggests that it was in FORTRAN 77. Though it seems that the NONE option came later.
    – badjohn
    Jun 8, 2018 at 13:05
  • Indeed, the facility to mess around with implicit types (making them even more confusing and error prone) was introduced in FORTRAN 77. Implicit none was introduced in Fortran 90. Curiously, implicit typing can be used for derived types (also introduced in 90). Why anyone thought that was a good idea is beyond me. Jun 8, 2018 at 13:37
  • @IanThompson Today, I prefer languages in which variables must be explicitly declared but, way back in the days when Fortran 77 was current, implicit declarations seemed like a good idea.
    – badjohn
    Jun 8, 2018 at 14:05
  • I can understand why they would be convenient when using punch cards, but why the feature was extended to derived types in F90 is beyond me. Jun 8, 2018 at 14:32

1 Answer 1

19

The use of such letters was common in mathematics long before programming existed. x, y, z were used as variables by Descartes in 1637; in his framework, a, b, c and other letters towards the start of the alphabet represented known values, letters towards the end of the alphabet represented unknown values. The use of i, j etc. doesn’t go quite that far back, but it was common at least for summation and other such operations, where the variables represent indices.

The original FORTRAN paper doesn’t give any specific arguments for the FORTRAN pattern, which is more than convention: variables in FORTRAN O could be of two types, fixed point (with a one- or two-symbol name starting with i, j, k, l, m, or n), or floating point (with a one- or two-symbol naming starting with any other character of the alphabet). Knuth notes that “the names of variables were restricted to be at most two characters long at this time; but this in itself was an innovation, FORTRAN being the first language in which a variable’s name could be larger than one letter, contrary to established mathematical conventions”. As can be seen in the rest of that paper, early programming languages closely followed mathematical representation, which explains the influence of the latter on the former.

There has been some amount of influence of computer programming notation on mathematical notation since then though; for example, some mathematical papers use words as variable names rather than single letters.

19
  • 1
    Over in maths, p and q are very popular for integers e.g. an archetypal rational number is p/q. Yet they are real by default in Fortran. Not surprising really, maths is too large and varied to expect an exact match. This question was motivated by one in the math SE in which someone commented that he preferred to reserve i, j, k for integers. Of course, another common use for i in maths is sqrt(-1). Which reminds me that Fortran is the only language that I know with a built-in complex type.
    – badjohn
    May 29, 2018 at 12:34
  • 2
    Both C and C++ have included complex types for a long time, but for whatever reason they've never seemed very popular.
    – supercat
    May 29, 2018 at 14:43
  • 1
    @badjohn Python has a built-in complex type. And uses j instead of i in complex literals for the imaginary part. I guess because that's more common in physics. j can be used as name though. As in j = 42+23j, which assigns a complex with 42 as real part and 23 as imaginary part to the name j.
    – BlackJack
    May 29, 2018 at 15:05
  • 1
    @BlackJack Thanks. Python has got on to my "need to look at" list but not yet my "have looked at list". I am used to j in electrical engineering but many branches of physics use i e.g. en.wikipedia.org/wiki/Schr%C3%B6dinger_equation. j as a variable and the imaginary unit - deliberate obfuscation?
    – badjohn
    May 29, 2018 at 15:14
  • 2
    @badjohn: At least in C, complex is a built-in primitive type (C doesn't have classes). Support for complex numbers, like support for variable-length arrays, was added in the C99 standard, but they're such widely popular features that a lot of programmers aren't even aware of them.
    – supercat
    May 29, 2018 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .