Not precisely, Daved.
A prototype (in the language of the standard this is a declaration of the function) is an interface specification: it gives the name of the function, the types of arguments, and return values. That is all the information a compiler needs in order to check that usage is valid with the interface. In C++, the compiler must have encountered a function declaration before it encounters code that attempts to call that function. The compiler makes sure usage maps the interface, and emits object code to do the call, but does not need the function implementation.
The function implementation (aka the definition of the function) is not needed where the function is called. It is (in rough terms) needed in order for the linker to be able to put compiled objects together, and produce an executable file. The linker simply generates linkages between the code that calls a function, and the entry point of that function. The important thing, however, is that the compiler does not need to have access to the function definition in order to call it.
What you described is a special case, and would look like this .....
Code:
include<iostream>
#include<iomanip>
using namespace std;
// Prototypes Here
double funcarea(double length, double width);
double funcperim(double length, double width);
int main()
{
double length;
double width;
double area;
double perimeter;
cout << "Enter length of lot";
cin >> length;
cout << "Enter width of lot";
cin >> width;
// do calculations
area = funcarea(length, width);
perimeter = funcperim(length, width);
cout << "The area of the lot is: " << area << endl;
cout << "The perimeter of the lot is: " << perimeter << endl;
return 0;
}
double funcarea(double length, double width)
{
return length * width;
}
double funcperim(double length, double width)
{
return length * 4;
}
(Note that I removed some syntax errors from MuzicMedia's code in the process of producing the above).
However, the compiler does not need the implementations of functions to do it's stuff. The more general way of using prototypes is to put functions into separate source files. A common way is to place the prototypes into a header file (let's call it func.h)
Code:
#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED
// Prototypes Here
double funcarea(double length, double width);
double funcperim(double length, double width);
#endif
and then have the source file for main() as follows;
Code:
#include<iostream>
#include<iomanip>
#include "func.h"
using namespace std;
int main()
{
double length;
double width;
double area;
double perimeter;
cout << "Enter length of lot";
cin >> length;
cout << "Enter width of lot";
cin >> width;
// do calculations
area = funcarea(length, width);
perimeter = funcperim(length, width);
cout << "The area of the lot is: " << area << endl;
cout << "The perimeter of the lot is: " << perimeter << endl;
return 0;
}
and a separate source file for implementing the functions.
Code:
#include "func.h" // bring in the prototypes
double funcarea(double length, double width)
{
return length * width;
}
double funcperim(double length, double width)
{
return length * 4;
}
The usage of the header files (which draws in the "prototypes") is not strictly needed in this second file, but is a VERY good idea practically in order to ensure that the function definitions correspond to their declarations (i.e. the function implementation match the prototypes).
Given the two source files and header, the sequence to build an executable would be;
1) Compile each source file, to produce corresponding object files.
2) Link the two object files (and any other required libraries or object files) together to produce the executable.
Note: before anyone accuses me of telling fibs, the approach has to be a bit different for template functions.