This is a non-technical question. I'm just curious why Mathematica breaks the convention that parentheses are widely used for function arguments. What's the advantage of f[x] over f(x)?

Again, for the derivative of a function, f'(x) and f''(x) are more familiar than f'[x] and f''[x]. I think these conventions in math textbooks have already existed for hundreds of years.

If function arguments are denoted as f(x), then array[i] could be used as array index. (c.f. Mathematica uses array[[i]] here.)

To quote from the official documentation:

The Four Kinds of Bracketing in the Wolfram Language

(term) parentheses for grouping

f[x] square brackets for functions

{a, b, c} curly braces for lists

v[[i]] double brackets for indexing (Part[v, i])

Are there any historical or antithetical reasons for choosing these notations?

  • 1
    "If function arguments are denotes as f(x), then array[i] could be used as array index.(c.f. Mathematica uses [[i]] here.)" — is (f + g)(x + y) parenthesizing and multiplying two terms or is it a function call (like (f + g)[x + y] in the current syntax)? – rm -rf Feb 1 '15 at 2:13
  • @rm-rf Your example is quite straight forward. Thank you! BTW. Your user name rm -rf is a dangerous yet powerful command. I miss it on Windows. – Nick Feb 1 '15 at 6:30
  • 3
    @rm-rf A few days ago I did a rm * accidentally (I meant rm *.pdf) and lost about 15GB of stuff :) – Rojo Feb 1 '15 at 20:45
  • A choice of ruling out ambiguity. – Αλέξανδρος Ζεγγ Mar 26 at 3:26
up vote 27 down vote accepted

The answer is quite simple. Most people want to multiply numbers without having to use the * symbol, e.g. 3x vs 3*x.

So given that this exists in Mathematica, using () for function arguments would introduce ambiguity.

Is f(x + y) meant to be f[x + y] or f*(x + y)?

This is actually a problem Wolfram|Alpha can face since we try to allow for all forms of inputs.

Other languages like C chose the other route, which means you must use * to indicate multiplication (something that annoys me). Given that Mathematica's original purpose was for mathematics, I think the right choice was made.

  • Though simple, good to know for the new commers. Thank you. – Nick Feb 1 '15 at 6:28
  • 5
    +1 This very example is discussed in The Mathematica Book. – WReach Feb 1 '15 at 17:41

Although Chip's answer already suffices to address the question, I would like to quote here a relevant part of the dialog by Theo Gray and Jerry Glynn in their book Exploring Mathematics with Mathematica; as there does not seem to be an easily accessible online version or preview of the book anywhere, I hope the quotation is useful:

Theo: Satisfied? Mathematica also knows about a whole bunch of other functions, such as trigonometric functions. One of the weird things about Mathematica that tends to annoy people for a while is that you have to use square brackets and capital letters. For example:

Sin[1.2]
   0.932039

Jerry: In other words, you're saying I can't type sin 1.2 with no parentheses, or sin(1.2), or Sin(1.2), or sin[1.2]. I must type Sin[1.2], exactly as you did. That seems like a real imposition.

Theo: Yes, you have to type Sin[1.2], exactly. There are good reasons for both requirements, and we'll see why in later chapters. If you use one of the variations you suggested above, Mathematica will warn you that you are probably making a mistake. All of your variations are legal Mathematica input, but they don't mean what you want. (For example, sin(1.2) means the variable named sin multiplied by 1.2.)

Jerry: OK, I'll live with the funny brackets for now.

Jerry: …now, what about square brackets? Why can't I use Sin(x) instead of Sin[x]?

Theo: Good question! There is, in fact, a good reason. Ordinary mathematical notation is inconsistent here. Round parentheses are used to mean two completely different things in traditional notation: first, order of evaluation; second, function arguments. Consider the expression k(b + c). Does this mean k times the quantity b + c, or does it mean the function k with the argument b + c? Unless you know from somewhere else that k is a function, or that k is a variable, you can't tell. It's a mistake to use the same symbols to mean these two completely different things, and Mathematica corrects this mistake by using round parentheses only for order of evaluation, and square brackets only for function arguments.

Jerry: That's a nice point. I never thought of that before. It shows how easily we adapt to nonsense. Aside from that, are you saying that mathematicians have been sloppy for centuries? That's a pretty strong statement!

Theo: Yes. Although I'm all in favor of interesting, quirky languages for writing novels and poetry (English comes to mind), it's really a bad idea to use an ambiguous language for something like mathematics. One of the great contributions of computer science to the world has been a powerful set of tools for thinking about what makes a language "good".

An alternative would be to insist on using a * for all multiplication. Then k(b + c) would always mean the function k, and if you wanted it to mean multiplication you would have to use k*(b + c). We decided it was better to remove an inconsistency than to force people to use an extra symbol. Another option would have been to have Mathematica "know" what was a variable and what was a function. This turns out to have serious consequences, and it's really not a good idea.

Jerry: Well, I didn't expect a lecture!

Theo: Sorry. Let's get back to the matter at hand. For functions, you use square brackets. Let's use the Sin function together with some round parentheses, to see how they fit:

Sin[1.2 (3 + 4)] (4 + 5)
   7.69139

Jerry: This means, Find the sine of 1.2 times 7 and multiply that answer by 9.

Theo: Yes.

  • "Another option would have been to have Mathematica "know" what was a variable and what was a function. This turns out to have serious consequences, and it's really not a good idea." Can someone elaborate on this – daniatic Jun 12 at 8:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.