Functions: Using Patterns in Delayed Assignment (:=)

Defining Functions with Patterns (This is an important section to understand well)

Defining functions with patterns probably combines the most useful aspects of Mathematica.  Define a function that takes patten matching x as its first argument and an argument matching n as its second argument and returns x to the "index_110.gif" power:

"index_111.gif"

"index_112.gif"

"index_113.gif"

"index_114.gif"

"index_115.gif"

This works fine, but suppose we had defined  x ahead of time

"index_116.gif"

"index_117.gif"

"index_118.gif"

RHS evaluated, it is x^(second argument) which is currently 4^(second argument)

"index_119.gif"

"index_120.gif"

"index_121.gif"

"index_122.gif"

Delayed Assignmet (:=)

The problem with the above functions is that the evaluation is made immediately.  For a function, the argument serves as a placeholder for a future calculation.  You do not wish to have the right–hand–side evaluated until the actual values of left–hand–side are specified.  The remedy is to use delayed assignment :=

"index_123.gif"

"index_124.gif"

"index_125.gif"

"index_126.gif"

"index_127.gif"

"index_128.gif"

"index_129.gif"

"index_130.gif"

"index_131.gif"

"index_132.gif"

"index_133.gif"

"index_134.gif"

"index_135.gif"

This is the paradigm for defining functions: A Pattern with a Delayed Assignment with that Pattern:
myFunction[pattern_] := pattern^2

Spikey Created with Wolfram Mathematica 6