views:

1062

answers:

13

It seems like all programming languages use commas (,) to separate function parameters.

Why don't they use just spaces instead?

+41  A: 

Absolutely not. What about this function call:

 function(a, b - c);

How would that look with a space instead of the comma?

 function(a b - c);

Does that mean function(a, b - c); or function(a, b, -c);? The use of the comma presumably comes from mathematics, where commas have been used to separate function parameters for centuries.

Carl Norum
function(a (b - c)); ? :)
Tim Mahy
In languages that do use space as a separator (lisp, ml, haskell), this would look like `function a (b - c)` (or `(function a (- b c))` in lisp).
sepp2k
So in those cases, parentheses are used for disambiguating the arguments. I was interpreting the OP's question as "Don't you think space would be a better separator in languages where commas are currently used". In that case, parentheses plus spaces could be used, but what's the advantage of that over the comma?
Carl Norum
If the language does it like e.g. Haskell and ML (defining `foo x y z = ...` as `foo x = \y -> (\z -> ...)` where \ starts a lambda, thus making partial application really easy), this can make some functional code much prettier. Consider `sumOfOddSquares = sum . filter odd . map (^2) $ [1..]` (taken from Learn You A Haskell)
delnan
Your example leads to ambiguity only because "-" is both a unary and a binary operator. If you get rid of this ambiguity, then using space becomes feasible. Just like with any other binary operator: function(a b ^ c) or function(a b 0 ^ c)
manixrock
+1 for mentioning Mathematics; that's definitely where this notation originated.
Donal Fellows
+8  A: 

Lisp-like languages use: (f arg1 arg2 arg3) which is essentially what you're asking for. ML-like languages use concatenation to apply curried arguments, so you would write f arg1 arg2 arg3.

Paul Hankin
I love it. Are there popular server side languages that have similar syntax?
Emanuil
@Emanuil Few languages have S-Expression, and none of them are very popular. Same applies for other functional languages, which are usually conceptually similar though have more conventional syntax.
delnan
@Emanuil popular? Depends on your definition. There are web frameworks for "popular" lisps, CL and scheme notably.
jeremiahd
@Emanuil: I believe you are looking at Clojure here; a healthy ecosystem of Java libraries along with the Lisp-like syntax which you so much adore ;-)
sasuke
@Emanuil: Ocaml and F# are "popular" ML dialects.
Mauricio Scheffer
+36  A: 

First of all, your premise is false. There are languages that use space as a separator (lisp, ML, haskell, possibly others).

The reason that most languages don't is probably that a) f(x,y) is the notation most people are used to from mathematics and b) using spaces leads to lots of nested parentheses (also called "the lisp effect").

sepp2k
Using less parenthesis for function calls doesn't lead to more parenthesis. What leads to lots of parenthesis is to build big expressions out of many parts that need to be grouped some way (in any language). In lisp parenthesis are basically the only structuring symbols, while other languages also use `,;.{}`... So since parenthesis fulfill the function of all these symbols, you of course get more of them (but less braces,...)
sth
Then there's the Haskell $ operator, useful for eliminating a set of parenthsis around a final term, e.g. sum (foo 3 (bar 23 5)) is equivalent to sum $ foo 3 $ bar 23 5.
Chris Smith
+2  A: 

This is not true. Some languages don't use commas. Functions have been Maths concepts before programming constructs, so some languages keep the old notation. Than most of the newer has been inspired by C (Javascript, Java, C#, PHP too, they share some formal rules like comma).

Charlie
+2  A: 

You don't have to look further than most of our natural languages to see that comma is used for separation items in lists. So, using anything other than comma for enumerating parameters would be unexpected for anyone learning a programming language for the first time.

Mladen Jablanović
+6  A: 

Tcl uses space as a separator between words passed to commands. Where it has a composite argument, that has to be bracketed or otherwise quoted. Mind you, even there you will find the use of commas as separators – in expression syntax only – but that's because the notation is in common use outside of programming. Mathematics has written n-ary function applications that way for a very long time; computing (notably Fortran) just borrowed.

Donal Fellows
+1 for tcl answer
TokenMacGuy
+2  A: 

This may sound a bit wise but I gather for the same reason why most earth-planet languages use it (english, french, and those few others ;-) Also, it is intuitive to most.

ldigas
Spoken languages don't use it... Just my 2 cents: It's just in the written language to support reading it easier.
hurikhan77
@hurikhan77 - I beg to differ. Try listening to someone who knows how to read out loud (most people don't). With a little concentration, you'll be able to tell perfectly well where in a sentence a comma came along.
ldigas
+4  A: 

There's a number of historical reasons already pointed out.

Also, it's because in most languages, where , serves as separator, whitespace sequences are largely ignored, or to be more exact, although they may separate tokens, they do not act as tokens themselves. This is moreless true for all languages deriving their syntax from C. A sequence of whitespaces is much like the empty word and having the empty word delimit anything probably is not the best of ideas.

Also, I think it is clearer and easier to read. Why have whitespaces, which are invisible characters, and essentially serve nothing but the purpose of formatting, as really meaningful delimiters. It only introduces ambiguity. One example is that provided by Carl.

A second would f(a (b + c)). Now is that f(a(b+c)) or f(a, b+c)?

The creators of JavaScript had a very useful idea, similar to yours, which yields just the same problems. The idea was, that ENTER could also serve as ;, if the statement was complete. Observe:

function a() {
    return "some really long string or expression or whatsoever";
}

function b() {
    return 
          "some really long string or expression or whatsoever";
}
alert(a());//"some really long string or expression or whatsoever"
alert(b());//"undefined" or "null" or whatever, because 'return;' is a valid statement

As a matter of fact, I sometimes tend to use the latter notation in languages, that do not have this 'feature'. JavaScript forces a way to format my code upon me, because someone had the cool idea, of using ENTER instead of ;.

I think, there is a number of good reasons why some languages are the way they are. Especially in dynamic languages (as PHP), where there's no compile time check, where the compiler could warn you, that the way it resolved an ambiguity as given above, doesn't match the signature of the call you want to make. You'd have a lot of weird runtime errors and a really hard life.

There are languages, which allow this, but there's a number of reasons, why they do so. First and foremost, because a bunch of very clever people sat down and spent quite some time designing a language and then discovered, that its syntax makes the , obsolete most of the time, and thus took the decision to eliminate it.

back2dos
+2  A: 

While some languages do use spaces, using a comma avoids ambiguous situations without the need for parentheses. A more interesting question might be why C uses the same character as a separator as is used for the "a then b" operator; the latter question is in some ways more interesting given that the C character set has at three other characters that do not appear in any context (dollar sign, commercial-at, and grave, and I know at least one of those (the dollar sign) dates back to the 40-character punchcard set.

supercat
+1  A: 

Haskell doesn't use spaces.

Example

multList :: [Int] -> Int -> [Int]
multList (x : xs) y = (x * y) : (multList xs y) 
multList [] _ = []

The reason for using commas in C/C++ is that reading a long argument list without a separator can be difficult without commas

Try reading this

void foo(void * ptr point & * big list<pointers<point> > * t)

commas are useful like spaces are. In Latin nothing was written with spaces, periods, or lower case letters.

Try reading this

IAMTHEVERYMODELOFAWHATDOYOUWANTNOTHATSMYBUCKET

it's primarily to help you read things.

BT
In your Haskell example, I think you meant Int-- literal type names always start with a capital letter. Also, in the expression side of the first case of multList, the call to multList isn't applied to enough arguments. Sorry if it sounds pedantic, but it helps when code examples are error-free.
Chris Smith
@Chris Smith - no you're right. I just coded that off my head in like two minutes without reading over it that much. Fixing it now.
BT
+1  A: 

It seems like all programming languages use commas (,) to separate function parameters.

In natural languages that include comma in their script, that character is used to separate things. For instance, if you where to enumerate fruits, you'd write: "lemon, orange, strawberry, grape" That is, using comma.

Hence, using comma to separate parameters in a function is more natural that using other character ( | for instance )

Consider:

someFunction( name, age, location ) 

vs.

someFunction( name|age|location )

Why don't they use just spaces instead?

Thats possible. Lisp does it.

The main reason is, space, is already used to separate tokens, and it's easier not to assign an extra functionality.

OscarRyz
+1  A: 

I have programmed in quite a few languages and while the comma does not rule supreme it is certainly in front. The comma is good because it is a visible character so that script can be compressed by removing spaces without breaking things. If you have space then you can have tabs and that can be a pain in the ... There are issues with new-lines and spaces at the end of a line. Give me a comma any day, you can see it and you know what it does. Spaces are for readability (generally) and commas are part of syntax. Mind you there are plenty of exceptions where a space is required or de rigueur. I also like curly brackets.

MikeAinOz
+1  A: 

It is probably tradition. If they used space they could not pass expression as param e.g.

f(a-b c)

would be very different from

f(a -b c)
fastcodejava
You just need to define a precedence for juxtaposition syntax function application. In Haskell, it's defined to be greater than any operator.
Chris Smith