Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 2nd, 2005, 7:51 AM   #1
Konnor
Newbie
 
Join Date: Aug 2005
Posts: 20
Rep Power: 0 Konnor is on a distinguished road
'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[])
{
someFunc(1,2);
} int someFunc(double arg1, int arg2) {
//......do something
}

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,
Konnor is offline   Reply With Quote
Old Sep 2nd, 2005, 8:02 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
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);

}
If the function definition is in the same source file as the caller, but AFTER the call point, then the prototype must be visible first, viz.
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);
}
grumpy is offline   Reply With Quote
Old Sep 4th, 2005, 3:32 AM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Sep 4th, 2005, 6:19 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC