'Date structure' question

Hi. I am studying 'Data and structure'. In this example I understood the structure of " printmovie ( movies_t movie(which can be mind and yours) )"
But I wondered why does 'Void printmovie (movies_t movie) has to show up in advance and again. Bit confused. would you explain why? thank you :]

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
38
 // 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";
}
Its called prototyping.

http://en.wikipedia.org/wiki/Function_prototype

To explain it in a wacky way.

the program always runs int main() first, always. So when it goes to main, and you call the function printmovie() The compilers gonna be like... UHH BROOO dafuq is this shit, I dont know anything about this. But if you prototype it, it'll see it, and have it in the back of its head. And once the function is called in main its like AHHHH i remember that shit, Imma go look for that function now.

Hope this helped :D
Last edited on
Thank you now it is much clear to me. I like the wacky way you explained to me haha. thank you!!
Topic archived. No new replies allowed.