The Student's Introduction to MATHEMATICA: A Handbook for Precalculus, Calculus, and Linear Algebra

A function is a rule that assigns to each input exactly one output. Many functions, such as the natural logarithm function Log, are built in to Mathematica. You provide an input, or argument, and Mathematica produces the output:
In[1]:= <b class="bold">Log[1]</b>Out[1]= 0
You can define your own function in Mathematica like this (use the BasicMathInput palette to type x 2; see Section 1.6, page 4):
In[2]:= <b class="bold">f[x_] := x</b><sup<b class="bold">2</b></sup><b class="bold"> +</b> <b class="bold">2x <span class="unicode">?</span> 4</b>
This function will take an input x, and output x 2 + 2 x ? 4. For instance:
In[3]:= <b class="bold">f[1]</b>Out[3]= <span class="unicode">?</span>1 In[4]:= <b class="bold">f[</b><i class="emphasis"><b class="bold"><span class="unicode">?</span></b></i><b class="bold">]</b>Out[4]= <span class="unicode">?</span>4 + 2<i class="emphasis"><span class="unicode">?</span></i> + <i class="emphasis"><span class="unicode">?</span></i><sup2</sup>
As a second example, here is a function that will return the multiplicative inverse of its argument (again, use the BasicMathInput palette to type the fraction):
<span class="inlinemediaobject"><img alt="Image from book"> border="0"> height="41"> id="IMG_84"> src="https://images.books24x7.com/bookimages/id_31099/fig63_01.jpg"> title=""> width="130"></span>
Let s try it:
In[6]:= <b class="bold">inv[45]</b><span class="inlinemediaobject"><img alt="Image from book"> border="0"> height="41"> id="IMG_85"> src="https://images.books24x7.com/bookimages/id_31099/fig63_02.jpg"> title=""> width="69"></span>
You can also create functions by combining existing functions:
In[7]:= <b class="bold">g[x_] := N[inv[x]]</b> In[8]:= <b class="bold">g[45]</b>Out[8]= 0.0222222
| Defining a Function | Follow these rules when defining a function:
|