Why code duplicate the

I will like comments about the tutorial section on Structure
http://www.cplusplus.com/doc/tutorial/structures/

The code has a void block that is incomplete or repeated after the main() block. I am referring to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  // example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} mine, yours;

void printmovie (movies_t movie);

int main ()
{
  string mystr;

  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;

  cout << "My favorite movie is:\n ";
  printmovie (mine);
  cout << "And yours is:\n ";
  printmovie (yours);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";


Why it repeats the void printmovie (movies_t movie); in line 12 then below the main() line 34 block repeats the printmovie? Is this an error?
Last edited on
Line 12 is called a function prototype. It is needed for the compiler to know that printmovie is a proper function so that it can be called before you actually define it (you call it on lines 28 and 30, but define it starting on line 34).

The alternative would be to move main below the definition of printmovie; if you did this, no prototype for printmovie would be needed.

Declarations, Prototypes, Definitions, and Implementations
http://www.cplusplus.com/articles/yAqpX9L8/

Functions
http://www.cplusplus.com/doc/tutorial/functions/
Topic archived. No new replies allowed.