Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node174_1.gif" "node174_2.gif" "node174_3.gif"

4.4.8    Function calls: prefix and postfix syntax

Apart from the standard way to apply a function to some expression, there exist two more short - hand forms  for a function of a single argument: prefix and postfix notation. In the prefix notation, the special symbol < @> is used, while in the postfix notation, the double slash is used : < // > . For example :



The prefix form is convenient  to remove extra square brackets  when there is a piece of code with deeply nested function calls, like here :



The postfix notation is convenient when we want to apply some operation  which is conceptually less important than the code it encloses, such as Timing measurements or rendering a matrix into a MatrixForm. In this way, it  does not interfere with the main code when we read it:



Another reason to use the postfix notation is that the order in which the function calls  are appearing  corresponds to the order in which the transformations are applied to input, and this may make the code more readable in some cases.

One has to be careful with both prefix and postfix forms due to precedence however, as the following examples illustrate:



In these cases, the result is such because the precedence of the subtraction or even Power operator is lower than that of the function call in the prefix notation. We have to use parentheses :



Another example





The determinant has not been computed because the < matr > variable stores not just the matrix, but the matrix wrapped in a MatrixForm. This can be verified by looking at the FullForm :



Once again, the parentheses must  be used :




Because of these precedence - related complications which often result in bugs, I would not recommend using these forms if this does not bring obvious advantages such as much improved code readability etc. Moreover, they are mostly used in interactive sessions and less so in  complete stand-alone programs.

"node174_23.gif" "node174_24.gif" "node174_25.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!