Skip to main content
Commonmark migration
Source Link

##Understand that semicolon (;) is not a delimiter

Understand that semicolon (;) is not a delimiter

Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the function Plus.

You can verify this by evaluating

Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]

CompoundExpression is necessary to Mathematica because many of the core programming functions such as SetDelayed (:=), Module, Block, and With take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression provides the construct that bundles an indefinite number of expressions into one.

Wolfram Research chose semicolon for the infix operator form of CompoundExpression to make Mathematica code look more like C code, but this is only syntactic sugar.

The only true delimiter in Mathematica is comma (,).

###Update

Update

One often sees code like the following

data = RandomReal[{0., 10.}, {1000, 2}];

The variable data is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null, which does not print in OutputForm. In the case of CompoundExpression, there is an additional twist -- I quote from the Mathematica documentation:

expr1; expr2; returns value Null. If it is given as input, the resulting output will not be printed. Out[n] will nevertheless be assigned to be the value of expr2.

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

keywords delimiter terminator separator semicolon compound-expression

##Understand that semicolon (;) is not a delimiter

Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the function Plus.

You can verify this by evaluating

Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]

CompoundExpression is necessary to Mathematica because many of the core programming functions such as SetDelayed (:=), Module, Block, and With take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression provides the construct that bundles an indefinite number of expressions into one.

Wolfram Research chose semicolon for the infix operator form of CompoundExpression to make Mathematica code look more like C code, but this is only syntactic sugar.

The only true delimiter in Mathematica is comma (,).

###Update

One often sees code like the following

data = RandomReal[{0., 10.}, {1000, 2}];

The variable data is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null, which does not print in OutputForm. In the case of CompoundExpression, there is an additional twist -- I quote from the Mathematica documentation:

expr1; expr2; returns value Null. If it is given as input, the resulting output will not be printed. Out[n] will nevertheless be assigned to be the value of expr2.

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

keywords delimiter terminator separator semicolon compound-expression

Understand that semicolon (;) is not a delimiter

Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the function Plus.

You can verify this by evaluating

Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]

CompoundExpression is necessary to Mathematica because many of the core programming functions such as SetDelayed (:=), Module, Block, and With take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression provides the construct that bundles an indefinite number of expressions into one.

Wolfram Research chose semicolon for the infix operator form of CompoundExpression to make Mathematica code look more like C code, but this is only syntactic sugar.

The only true delimiter in Mathematica is comma (,).

Update

One often sees code like the following

data = RandomReal[{0., 10.}, {1000, 2}];

The variable data is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null, which does not print in OutputForm. In the case of CompoundExpression, there is an additional twist -- I quote from the Mathematica documentation:

expr1; expr2; returns value Null. If it is given as input, the resulting output will not be printed. Out[n] will nevertheless be assigned to be the value of expr2.

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

keywords delimiter terminator separator semicolon compound-expression

comments noted that this should be infix, not binary
Source Link

##Understand that semicolon (;) is not a delimiter

Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the function Plus.

You can verify this by evaluating

Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]

CompoundExpression is necessary to Mathematica because many of the core programming functions such as SetDelayed (:=), Module, Block, and With take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression provides the construct that bundles an indefinite number of expressions into one.

Wolfram Research chose semicolon for the binaryinfix operator form of CompoundExpression to make Mathematica code look more like C code, but this is only syntactic sugar.

The only true delimiter in Mathematica is comma (,).

###Update

One often sees code like the following

data = RandomReal[{0., 10.}, {1000, 2}];

The variable data is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null, which does not print in OutputForm. In the case of CompoundExpression, there is an additional twist -- I quote from the Mathematica documentation:

expr1; expr2; returns value Null. If it is given as input, the resulting output will not be printed. Out[n] will nevertheless be assigned to be the value of expr2.

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

keywords delimiter terminator separator semicolon compound-expression

##Understand that semicolon (;) is not a delimiter

Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the function Plus.

You can verify this by evaluating

Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]

CompoundExpression is necessary to Mathematica because many of the core programming functions such as SetDelayed (:=), Module, Block, and With take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression provides the construct that bundles an indefinite number of expressions into one.

Wolfram Research chose semicolon for the binary operator form of CompoundExpression to make Mathematica code look more like C code, but this is only syntactic sugar.

The only true delimiter in Mathematica is comma (,).

###Update

One often sees code like the following

data = RandomReal[{0., 10.}, {1000, 2}];

The variable data is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null, which does not print in OutputForm. In the case of CompoundExpression, there is an additional twist -- I quote from the Mathematica documentation:

expr1; expr2; returns value Null. If it is given as input, the resulting output will not be printed. Out[n] will nevertheless be assigned to be the value of expr2.

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

keywords delimiter terminator separator semicolon compound-expression

##Understand that semicolon (;) is not a delimiter

Although it may look to newcomers that semicolons are used in Mathematica as statement terminators as in C or Java, or perhaps as statement separators as in Pascal and its derivatives, in fact, semicolons are the infix form of the function CompoundExpression, just as plus-signs (+) are the infix form of the function Plus.

You can verify this by evaluating

Hold[a; b; c] // FullForm
Hold[CompoundExpression[a, b, c]]

CompoundExpression is necessary to Mathematica because many of the core programming functions such as SetDelayed (:=), Module, Block, and With take only a single expression as their second argument. This second argument is of course the code body and normally requires the evaluation of many expressions. CompoundExpression provides the construct that bundles an indefinite number of expressions into one.

Wolfram Research chose semicolon for the infix operator form of CompoundExpression to make Mathematica code look more like C code, but this is only syntactic sugar.

The only true delimiter in Mathematica is comma (,).

###Update

One often sees code like the following

data = RandomReal[{0., 10.}, {1000, 2}];

The variable data is probably going to be used as test or example data. The semicolon is added to suppress the output from this Set expression because the output is both very large and no one really cares about its details. Because there is no visible output, it would be easy to assume the expression returns nothing, but that is not true. Mathematica expressions always return something, even if it is only the token Null, which does not print in OutputForm. In the case of CompoundExpression, there is an additional twist -- I quote from the Mathematica documentation:

expr1; expr2; returns value Null. If it is given as input, the resulting output will not be printed. Out[n] will nevertheless be assigned to be the value of expr2.

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

keywords delimiter terminator separator semicolon compound-expression

Removed update that should be a different answer.
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Hold[CompoundExpression[a, b, c]]

Hold[CompoundExpression[a, b, c]]

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

###Omitting ; can cause unexpected results in functions

A missing ; somewhere inside a function is my number one reason for time-consuming debugging because there are no error messages indicating that something is wrong and where the error is. Here a simple example:

Assume you write a function that performs some iterative calculation in a While loop and once some condition is fulfilled it returns the result (in the example just the input variable). To see the progress of your function you print the iteration counter at every iteration, but forget to put a semicolon at the end of the print command.

f1[x_] := Block[{counter},
   counter = 0;
   While[counter < 5,
    counter += 1;
    Print[counter]
     (* Note the missing ; after the Print[] command. 
     This will not throw an error. *)
     If[counter > 2, Return[x]];
    ];
   ];

Now you execute f1[20]. It prints

In[1]: f1[20]

1

2

3

4

5

and generates no output. It seems that the Return[x] part of the code did not work. However, the If[] command works just fine, as can be seen when putting some debug print message just there:

f2[x_] := Block[{counter},
   counter = 0;
   While[counter < 5,
    counter += 1;
    Print[counter]
     (* Note the missing ; after the Print[] command. 
     This will not throw an error. *)
     If[counter > 2, Print["if has been successfully evaluated"]; 
      Return[x]];
    ];
   ];

With the result

In[1]: f2[20]

1

2

3

if has been successfully evaluated

4

if has been successfully evaluated

5

if has been successfully evaluated

The problem is that Return behaves differently if there is no ; after the Print command somewhere up in the chain. In fact, due to the missing ; the Return only returns from the not properly bundled statement Print[counter]If[...] (remember that newlines are just decorations) and not from the actual function. Fixing the issue solves the problem:

f3[x_] := Block[{counter},
   counter = 0;
   While[counter < 5,
    counter += 1;
    Print[counter];
    (* Note the missing ; after the Print[] command. 
    This will not throw an error. *)
    If[counter > 2, Print["if has been successfull evaluated"]; 
     Return[x]];
    ];
   ];

In[1]: f3[20]

1

2

3

if has been successfully evaluated

Out[1]: 20

It is possible to see the change by the different indent of what comes after the Print.

Hold[CompoundExpression[a, b, c]]

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

###Omitting ; can cause unexpected results in functions

A missing ; somewhere inside a function is my number one reason for time-consuming debugging because there are no error messages indicating that something is wrong and where the error is. Here a simple example:

Assume you write a function that performs some iterative calculation in a While loop and once some condition is fulfilled it returns the result (in the example just the input variable). To see the progress of your function you print the iteration counter at every iteration, but forget to put a semicolon at the end of the print command.

f1[x_] := Block[{counter},
   counter = 0;
   While[counter < 5,
    counter += 1;
    Print[counter]
     (* Note the missing ; after the Print[] command. 
     This will not throw an error. *)
     If[counter > 2, Return[x]];
    ];
   ];

Now you execute f1[20]. It prints

In[1]: f1[20]

1

2

3

4

5

and generates no output. It seems that the Return[x] part of the code did not work. However, the If[] command works just fine, as can be seen when putting some debug print message just there:

f2[x_] := Block[{counter},
   counter = 0;
   While[counter < 5,
    counter += 1;
    Print[counter]
     (* Note the missing ; after the Print[] command. 
     This will not throw an error. *)
     If[counter > 2, Print["if has been successfully evaluated"]; 
      Return[x]];
    ];
   ];

With the result

In[1]: f2[20]

1

2

3

if has been successfully evaluated

4

if has been successfully evaluated

5

if has been successfully evaluated

The problem is that Return behaves differently if there is no ; after the Print command somewhere up in the chain. In fact, due to the missing ; the Return only returns from the not properly bundled statement Print[counter]If[...] (remember that newlines are just decorations) and not from the actual function. Fixing the issue solves the problem:

f3[x_] := Block[{counter},
   counter = 0;
   While[counter < 5,
    counter += 1;
    Print[counter];
    (* Note the missing ; after the Print[] command. 
    This will not throw an error. *)
    If[counter > 2, Print["if has been successfull evaluated"]; 
     Return[x]];
    ];
   ];

In[1]: f3[20]

1

2

3

if has been successfully evaluated

Out[1]: 20

It is possible to see the change by the different indent of what comes after the Print.

Hold[CompoundExpression[a, b, c]]

This the only case I know of where evaluating an expression at toplevel doesn't assign its actual output to Out[n].

Explained the issue of missing ; in functions
Source Link
Felix
  • 3.9k
  • 13
  • 33
Loading
Bounty Ended with 500 reputation awarded by ciao
added 3 characters in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
Added a section discussing semicolon at the end of an expression
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
deleted 20 characters in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
Added missing word
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259
Loading
Post Made Community Wiki by m_goldberg