开发者

Why do programming languages use commas to separate function parameters?

It seems like all programming languages use commas (,) to separate function parameters开发者_如何学运维.

Why don't they use just spaces instead?


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.


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").


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.


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.


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.


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.


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.


Haskell doesn't use commas.

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.


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).


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.


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.


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.


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)


Some languages, like Boo, allow you to specify the type of parameters or leave it out, like so:

def MyFunction(obj1, obj2, title as String, count as Int):       
    ...do stuff...

Meaning: obj1 and obj2 can be of any type (inherited from object), where as title and count must be of type String and Int respectively. This would be hard to do using spaces as separators.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜