WOLFRAM

2 Introducing Functions

2Introducing 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.1Compute 7+6+5 using the function Plus»
Expected output:
Out[]=
2.2Compute 2×(3+4) using Times and Plus»
Expected output:
Out[]=
2.3Use Max to find the larger of 6×8 and 5×9. »
Expected output:
Out[]=
2.4Use RandomInteger to generate a random number between 0 and 1000. »
Sample expected output:
Out[]=
2.5Use Plus and RandomInteger to generate a number between 10 and 20. »
Sample expected output:
Out[]=
+2.1Compute 5×4×3×2 using Times»
Expected output:
Out[]=
+2.2Compute 23 using Subtract»
Expected output:
Out[]=
+2.3Compute (8+7)*(9+2) using Times and Plus»
Expected output:
Out[]=
+2.4Compute (2689)/9 using Subtract and Divide»
Expected output:
Out[]=
+2.5Compute 1005^2 using Subtract and Power»
Expected output:
Out[]=
+2.6Find the larger of 3^5 and 5^3. »
Expected output:
Out[]=
+2.7Multiply 3 and the larger of 4^3 and 3^4. »
Expected output:
Out[]=
+2.8Add two random numbers each between 0 and 1000. »
Sample expected output:
Out[]=
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 functionslike Max or RandomIntegerthere’s no special form like +, so you have to give their names.
Can I mix Plus[ ...] and +?
Yes. Things like Plus[4+5, 2+3] or, for that matter, Plus[4, 5]*5 are just fine.
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 (23)4 and 2(34)).
  • 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.
Next Section