C Head files and Source files
<p>I am a student programmer learning my first language with the book "Accelerated C++." I am at at a point where the author is explaining header files and source files containing functions. In the exercise the book provides for explanation, the author has header files that contain the definitions for functions, but it seems redundant as the source file also has the definition for the functions. What is the point of a header file in this case  when programming C++?</p>

<p>Example: Header file.</p>

<pre><code>#ifndef MEDIAN_H
#define MEDIAN_H

#include &lt;vector&gt;
double median(std::vector&lt;double&gt;);

#endif // MEDIAN_H
</code></pre>

<p>Then source file containing function to determine the median of grades:</p>

<pre><code>// source file for the `median' function
#include &lt;algorithm&gt;    // to get the declaration of `sort'
#include &lt;stdexcept&gt;    // to get the declaration of `domain_error'
#include &lt;vector&gt;       // to get the declaration of `vector'

using std::domain_error;   using std::sort;   using std::vector;

#include "median.h"

// compute the median of a `vector&lt;double&gt;'
// note that calling this function copies the entire argument `vector'
double median(vector&lt;double&gt; vec)
{
#ifdef _MSC_VER
    typedef std::vector&lt;double&gt;::size_type vec_sz;
#else
    typedef vector&lt;double&gt;::size_type vec_sz;
#endif

    vec_sz size = vec.size();
    if (size == 0)
        throw domain_error("median of an empty vector");

    sort(vec.begin(), vec.end());

    vec_sz mid = size/2;

    return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
</code></pre>

<p>The median.h is copied into the median function source file even though the source already has the definition <code>vector&lt;double&gt; vec</code>
The book explains it as harmless and actually a good idea. But I just want to get a better understanding of the reason for such redundancy. Any explanation would be great!</p>
