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 6000 functions built into the Wolfram Language. Arithmetic uses just a very few of these.
Compute 3+4 using the function Plus:
Plus[3, 4]
Compute 1+2+3 using Plus:
Plus[1, 2, 3]
The function Times does multiplication:
Times[2, 3]
You can put functions inside other functions:
Times[2, Plus[2, 3]]
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:
Max[2, 7, 3]
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:
RandomInteger[100]
Each time you ask, you get another random number:
RandomInteger[100]
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. »
Do I have to type the capital letters in Plus, RandomInteger, etc.?
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.
Do I have to type square brackets [ ...] when I use functions?
Yes. Square brackets [ ...] are for functions; parentheses (...) are for grouping, as in 2*(3+4), not for functions.
How can I tell if my brackets [ ...] are matched?
Unmatched brackets normally get colored purple. If you click after a [ or before a ], its matching bracket will be indicated with a yellow background. In any correct input, every [ has to be matched with a ].
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.