title: Define variable local to function
body: <p>I am working (joyfully) working through the <em>Introduction to Emacs Lisp Programming</em> and have solved the first <a href="https://www.gnu.org/software/emacs/manual/html_node/eintr/search-Exercises.html#search-Exercises" rel="nofollow noreferrer">8.7 Searching Exercise</a>.  It states,</p>
  
  <blockquote>
    <p>Write an interactive function that searches for a string. If the
    search finds the string, leave point after it and display a message
    that says “Found!”.</p>
  </blockquote>
  
  <p>My solution is</p>
  
  <pre class="lang-el prettyprint-override"><code>(defun test-search (string)
    "Searches for STRING in document.
  Displays message 'Found!' or 'Not found...'"
    (interactive "sEnter search word: ")
    (save-excursion
      (beginning-of-buffer)
      (setq found (search-forward string nil t nil))) 
    (if found
        (progn
          (goto-char found)
          (message "Found!"))
      (message "Not found...")))
  </code></pre>
  
  <p>How do I make <code>found</code> be local to the function?  I know that a <code>let</code> statement defines a local variable.  However, I only want to move point if the <code>string</code> is found.  It's not clear to me how to define <code>found</code> locally, yet not have the point be set to the <code>beginning-of-buffer</code> if the <code>string</code> isn't found.  Is <code>let</code> the proper command for this situation?</p>