title: Writing functions in Prolog
body: <p>I am working with SWI Prolog. I wish to define an add function: <code>add(X, Y)</code> which returns the sum of X and Y. However, I do not know how to define functions in Prolog. I tried doing this using predicates as such:</p>
  
  <pre><code>add(X, Y, Z) :- Z is X+Y.
  </code></pre>
  
  <p>but upon executing a query of the form <code>add(2, 3, X)</code> this gives an error saying:</p>
  
  <blockquote>
    <p>ERROR: toplevel: Undefined procedure: add/3 (DWIM could not correct goal)</p>
  </blockquote>
  
  <p>Also, I cannot understand the difference between <code>:-</code> and <code>:=</code> while writing rules. I read somewhere that <code>:-</code> is used to define predicates while <code>:=</code> is used to define functions. I am not sure if this is correct. I tried using <code>:=</code> for defining functions but it doesn't work.</p>