Hi
Im pretty new to the whole C++ programming language but after learning python and java i decided to give it a go. I have been using tutorials at www.cplusplus.com and that has been working fine but i came across a bit of code today i couldn't understand. It went like this:

// 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";
}

My problem is where is goes void printmovie (movies_t movie); and there is nothing after it and then later in the program is does the same thing again but this time there is an actual function defined. My question is, why would the person who made this have put that void printmovie (movies_t movie); without having any actual code defined until way later in the program.
Thanks. :)

Recommended Answers

All 4 Replies

void printmovie (movies_t movie);

Is a function declaration.

The definition exists elsewhere in the same file.

Short/Simple reason: C++ can be very linear. If you comment out the function declaration, you will get a compiler error. That is because the function printmovie is found after the definition of main, but main is evaluated first during parsing and the compiler wont find the definition of printmovie until after main ( in this case the compiler will most likely flag an error, but then again this is completely dependent on the settings of the compiler).

I'm sure there are some compilers that can be configured to evaluate a function call and determine if it exists somewhere in a document without a need to evaluate the function declaration first, but I have yet to see such a compiler and even if one did exist it would be poor programming practice to code around what one compiler would accept vs multiple popular and conforming compilers.

If you want to use the function printmovie without the declaration, you will have to place the definition of printmovie before main.

is there any advantage to placing it after main? I mean is there any particular reason why it would have been after?

is there any advantage to placing it after main? I mean is there any particular reason why it would have been after?

I personally consider code in a Driver Program to be cleaner when the logic of main is known before anything else.

Also, function declarations make code more readable because you know of the functions that are in the program before they are defined (at least hopefully they are defined, otherwise its just unnecessary information).

Other than that, I'm not entirely sure. Though I do know that you don't have to declare a function elsewhere in the same file, you can do so in other files but it would require you to also tag on the word extern to your function declaration to send a flag to your compiler stating the function is defined in a separate file.

Thanks alex, thats really helpful. Thats all my problems solved.. for the moment

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.