2 | Introducing Functions |
When you type 2+2, the Wolfram Language understands it as Plus[2,2]. Plus is a function. There are more than 5000 functions built into the Wolfram Language. Arithmetic uses just a very few of these.
Compute 3+4 using the function Plus:
In[1]:= |
Out[1]= |
Compute 1+2+3 using Plus:
In[2]:= |
Out[2]= |
The function Times does multiplication:
In[3]:= |
Out[3]= |
You can put functions inside other functions:
In[4]:= |
Out[4]= |
All functions in the Wolfram Language use square brackets, and have names that start with capital letters.
The function Max finds the maximum, or largest, of a collection of numbers.
The maximum of these numbers is 7:
In[5]:= |
Out[5]= |
The function RandomInteger picks a random integer (whole number) between 0 and whatever size you say.
Pick a random whole number between 0 and 100:
In[6]:= |
Out[6]= |
Each time you ask, you get another random number:
In[7]:= |
Out[7]= |
Plus[2,2] | 2+2 | addition | ||
Subtract[5,2] | 5-2 | subtraction | ||
Times[2,3] | 2*3 | multiplication (2 3 also works) | ||
Divide[6,2] | 6/2 | division | ||
Power[3,2] | 3^2 | raising to a power | ||
Max[3,4] | maximum (largest) | |||
Min[3,4] | minimum (smallest) | |||
RandomInteger[10] | random whole number |
+2.6Find the larger of 3^5 and 5^3. »
+2.7Multiply 3 and the larger of 4^3 and 3^4. »
+2.8Add two random numbers each between 0 and 1000. »
Yes. In the Wolfram Language, plus is not the same as Plus. The capital letter in Plus signifies that you’re talking about the built-in (“official”) plus function.
Yes. Square brackets [...] are for functions; parentheses (...) are for grouping, as in 2*(3+4), not for functions.
How does one read Plus[2, 3] out loud?
Usually “plus of 2 and 3”; sometimes “plus of 2 comma 3”. “[” can be read as “open bracket”; “]” as “close bracket”.
Why use Plus[2, 3] instead of 2+3?
For Plus, it’s not necessary. But for the vast majority of functions—like Max or RandomInteger—there’s no special form like +, so you have to give their names.
Can I mix Plus[...] and +?
It means you’ve typed something that the Wolfram Language can’t understand. See Section 47 for more information. Start by checking that your open and close brackets are matched.
- Expressions in the Wolfram Language (see Section 33) consist of nested trees of functions.
- Plus can add any number of numbers, but Subtract only subtracts one number from another (to avoid ambiguities between (2−3)−4 and 2−(3−4)).
- The notion of a function is considerably more general in the Wolfram Language than in either traditional mathematics or computer science. For example, f[anything] is considered a function, whether it evaluates to something definite or remains in symbolic form.