Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how

Legacy Documentation

Mathematica 8 (2010)

This is documentation for Mathematica 8, which was
based on an earlier version of Wolfram Language.
View current documentation (Version 14.3)

Defining Functions

There are many functions that are built into Mathematica. This tutorial discusses how you can add your own simple functions to Mathematica.
As a first example, consider adding a function called which squares its argument. The Mathematica command to define this function is . The (referred to as "blank") on the left-hand side is very important; what it means will be discussed below. For now, just remember to put a on the left-hand side, but not on the right-hand side, of your definition.
This defines the function . Notice the on the left-hand side.
squares its argument.
Out[2]=
The argument can be a number.
Out[3]=
Or it can be a more complicated expression.
Out[4]=
You can use in a calculation.
Out[5]=
This shows the definition you made for .
f[x_]:=x^2 define the function
?f show the definition of
Clear[f] clear all definitions for

Defining a function in Mathematica.

The names like that you use for functions in Mathematica are just symbols. Because of this, you should make sure to avoid using names that begin with capital letters, to prevent confusion with built-in Mathematica functions. You should also make sure that you have not used the names for anything else earlier in your session.
Mathematica functions can have any number of arguments.
You can use the function just as you would any of the built-in functions.
Out[8]=
This gives a new definition for , which overwrites the previous one.
The new definition is displayed.
This clears all definitions for .
When you have finished with a particular function, it is always a good idea to clear definitions you have made for it. If you do not do this, then you will run into trouble if you try to use the same function for a different purpose later in your Mathematica session. You can clear all definitions you have made for a function or symbol f by using Clear [f].


ja zh

AltStyle によって変換されたページ (->オリジナル) /