Welcome to TagMyCode

Please login or create account to add a snippet.
Language: C++
Posted by: Khalil Johnson
Added: Dec 8, 2014 10:45 AM
Views: 1849
Tags: no tags
  1. #include <iostream>
  3. int add(int x, int y); // forward declaration of add() using a function prototype
  5. int main()
  6. {
  7.     using namespace std;
  8.     cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
  9.     return 0;
  10. }
  12. int add(int x, int y)
  13. {
  14.     return x + y;
  15. }


Khalil Johnson 8 years ago
A function prototype is declaration statement that tells the compiler what a function’s return type is, what the name of the function is, and what the types of it’s parameters are. A function prototype can be used to forward declare a function. A forward declaration is when something (such as a function or class) is declared in advance of where it is implemented.
Khalil Johnson 8 years ago
When the compiler is compiling a code file, it does not know about the existence of functions that live in any other files. This is done so that files may have functions or variables that have the same names as those in other files without causing a conflict.

However, in this case, we want main.cpp to know about (and use) the add() function that lives in add.cpp. To give main.cpp access to the add function, we can use a forward declaration

Write a comment