Hi Mac,
To expand a little on Eleazar's answer.
As a newcomer to WL it is important to understand that unlike most programming languages, WL uses term rewriting. When you define a function, you are actually defining a rewriting or replacement rule.
This defines a replacement where f[<something>] is replaced by <something>^2.
Clear[f, a, b]
f[x_] := x^2
f[2]
(* 4 *)
f[a + b]
(* (a + b)^2 *)
A replacement rule may also restrict the patterns to which it applies. This definition will only apply if the argument to f
is an Integer
. a + b
is not an Integer
so no replacement is performed (output = input).
Clear[f, a, b]
f[x_Integer] := x^2
f[2]
(* 4 *)
f[a + b]
(* f[a + b] *)
If you are interested in exploring this aspect of the language further, read this post and the many links it references.