You don't need to define a separate function (command) for this. And even if you did define one, it need not use
The standard command
query-replace does just what you request, if you provide it with a prefix argument.
So just use
C-u M-% to query-replace words.
C-h f query-replace tells you this (note the part in bold):
query-replace is an interactive compiled Lisp function in
It is bound to
<menu-bar> <edit> <replace> <query-replace>.
(query-replace FROM-STRING TO-STRING &optional DELIMITED START END
Replace some occurrences of FROM-STRING with TO-STRING.
As each match is found, the user must type a character saying
what to do with it. For directions, type C-h at that time.
In Transient Mark mode, if the mark is active, operate on the contents
of the region. Otherwise, operate from point to the end of the buffer.
Use M-n to pull the last incremental search string to the minibuffer
that reads FROM-STRING, or invoke replacements from
incremental search with a key sequence like
C-s C-s M-%
to use its current search string as the string to replace.
Matching is independent of case if
case-fold-search is non-nil and
FROM-STRING has no uppercase letters. Replacement transfers the case
pattern of the old text to the new text, if
case-fold-search are non-nil and FROM-STRING has no uppercase
letters. (Transferring the case pattern means that if the old text
matched is all caps, or capitalized, then its replacement is upcased
Ignore read-only matches if
query-replace-skip-read-only is non-nil,
ignore hidden matches if
search-invisible is nil, and ignore more
replace-lax-whitespace is non-nil, a space or spaces in the string
to be replaced will match a sequence of whitespace chars defined by the
Third arg DELIMITED (prefix arg if interactive), if non-nil, means replace
only matches surrounded by word boundaries. A negative prefix arg means
Fourth and fifth arg START and END specify the region to operate on.
To customize possible responses, change the bindings in
And if you insist that you want a separate command for this, then just do the obvious:
(defun q-r-word ()
"Query-replace whole words."
(let ((current-prefix-arg t))