![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 20
Rep Power: 0
![]() |
'function prototype'....
Just been reading up on what this actually means. The book gives an example of a function prototype.....
int someFunc(double, int);
int main(int nNumberofArgs, char* pszArgs[])
{does the line 'int someFunc(double, int);' always have to appear before the main? If the someFunc function was above the main, would this function prototype have to be used? or does position in the program not matter. Also, they use a function example earlier in the chapter. Could someone point out where the 'function prototype' is in this example? Hopefully the comments in the code provide an accurate enough desription of the program..... // SquareDemo - demonstrate the use of a function
// which processes arguments
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// square - returns the square of its argument
// doubleVar - the value to be squared
// returns - square of doubleVar
double square(double doubleVar)
{
return doubleVar * doubleVar;
}
// sumSequence - accumulate the square of the number
// entered at the keyboard into a sequence
// until the user enters a negative number
double sumSequence(void)
{
// loop forever
double accumulator= 0.0;
for(;;)
{
// fetch another number
double dValue = 0;
cout << "Enter next number: ";
cin >> dValue;
// if it's negative...
if (dValue < 0)
{
// ...then exit from the loop
break;
}
// ...otherwise calculate the square
double value = square(dValue);
// now add the square to the
// accumulator
accumulator= accumulator + value;
}
// return the accumulated value
return accumulator;
}
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "This program sums multiple series\n"
<< "of numbers squared. Terminate each sequence\n"
<< "by entering a negative number.\n"
<< "Terminate the series by entering two\n"
<< "negative numbers in a row\n"
<< endl;
// Continue to accumulate numbers...
double accumulatedValue;
for(;;)
{
// sum a sequence of numbers entered from
// the keyboard
cout << "Enter next sequence" << endl;
accumulatedValue = sumSequence();
// terminate if the sequence is zero or negative
if (accumulatedValue <= 0.0)
{
break;
}
// now output the accumulated result
cout << "\nThe total of the values squared is "
<< accumulatedValue
<< endl;
}
cout << "Thank you" << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE");
return 0;
}much obliged, |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
If the function is defined (i.e. it's body is implemented) in another source file, then the caller needs to see a function declaration (aka a prototype) before it can call it. The compiler must encounter the declaration before (i.e. above) the attempt to call the function. This is strictly enforced in C++ but is optional in C [at least in the 1989 C standard], but is usually considered good style in C.
If the function definition and the caller are in the same source file, things get a bit more problematical. If the function definition is above the call point, than that function definition implicitly provides a prototype. And the compiler will complain bitterly (about a function that is already defined) if a function prototype occurs after the definition, viz. int someFunc(double x, int y)
{
return something(x, y);
}
int someFunc(double, int); // error: someFunc is already defined
int main(int nNumberofArgs, char* pszArgs[])
{
someFunc(1,2);
}int someFunc(double, int); // required so we call it
int main(int nNumberofArgs, char* pszArgs[])
{
someFunc(1,2);
}
int someFunc(double x, int y)
{
return something(x, y);
} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
the prototype gives the return type, the name of the function, and the types of the various parameters that are passed to the function. it's just a template for the program to use to plug in your values to return a value. it doesn't say what the function does, just how to start it up.
int function(int x, int y) is a function called function that takes two integer parameters...x and y, and then returns an integer. it could be adding them, multiplying them, or plugging them into a complicated physics function that outputs anything (as long as it's an integer).
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The program doesn't use the prototype at all. It's for the compiler. It is used by the compiler to make sure you used the function properly when it encounters such usage in the remainder of the code. If nothing "official", such as a declaration (prototype) or a definition has been found at the time a C compiler encounters a use, it makes some assumptions. C++ is tougher, it won't compile without one or the other having preceded the usage. Grumpy's answer is quite complete.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|