Question about Functions

I am reading the book "C++ WITHOUT FEAR" by "BRIAN OVERLAND". I am in the fourth chapter, which is on functions. According to the last chapter in the book, my last code example as a local function.
/*
#include
#include
using namespace std;
// Function must be declared before being used.
int prime(int n);

int main()
{
*
But now this chapter is telling me about global functions. Which states that it is global when it is declared before main(). But the code above was a local function according to the book. This code block was written before we got to the introduction of global functions. I would love some help here, and thanks.

Comments

  • [color=Blue]
    1. Functions should have prototypes - in some cases (I will talk about it later on) it may not be needed, but always have function prototype is a very good coding practice. Prototype is a full function declaration, but instead of a code for a function - it ends with the ';' character, like all C statements. Here is an example:
    [code]
    [color=Green]//
    // Function prototype
    //[/color]
    int GetMoreData (char* pszFileName, int iLoadingOptions)[color=Red];[/color]

    ...

    [color=Green]//
    // Function body of code
    //[/color]
    int GetMoreData (char* pszFileName, int iLoadingOptions)
    [color=Red]{[/color]
    [color=Green]// Your code here...[/color]
    [color=Red]}[/color]
    [/code]
    It is easy to convert the code body into prototype and back.

    2. Placement of prototypes - all prototypes must be declared [italic]before[/italic] the use of the function. The best way is to do it AFTER the include statements are made, but before any code is written:
    [code]
    #include <...>
    #include <...>
    #include <...>

    [color=Green]//
    // Function Prototypes
    //[/color]

    ...

    [color=Green]//
    // Rest of the code in this file
    //[/color]

    int main (int argc, char* argv [])
    {
    return 0;
    }
    [/code]
    3. Prototype is not needed if the function body precedes its use - if the function called AFTER its body has been defined:
    [code]
    void [color=Red]function1[/color] (int n1)
    {
    }

    void function2 (int n2)
    {
    [color=Red]function1[/color] (n2);
    }
    [/code]
    However, this may not compile without the prototype:
    [code]
    void function1 (int n1)
    {
    [color=Red]function2[/color] (n1);
    }

    void [color=Red]function2[/color] (int n2)
    {
    }
    [/code]
    This is why all the prototypes should be in the beginning of the file. Otherwise, if you have to use functions without knowing the locations - you have to move code back and forth all the time.

    4. The bodies of functions may be mixed in any order in the file. The location of [italic]main()[/italic] does not matter. It may be in the beginning, the end or in the middle of all other functions. All the functions are global unless [italic]static[/italic][color=Red][/color] keyword is used:
    [code]
    [color=Green]//
    // Function prototype
    //[/color]
    static int GetMoreData (char* pszFileName, int iLoadingOptions)[color=Red];[/color]

    ...

    [color=Green]//
    // Function body of code
    //[/color]
    static int GetMoreData (char* pszFileName, int iLoadingOptions)
    [color=Red]{[/color]
    [color=Green]// Your code here...[/color]
    [color=Red]}[/color]
    [/code]
    Functions (and variables) declared with [italic]static[/italic] cannot be used from other C/C++ files, therefore they are local objects. In other words: locality of the function is not in the location relative to [italic]main()[/italic], but in using [italic]static[/italic] keyword.
    [/color]
  • : #include
    : #include
    : using namespace std;
    : // Function must be declared before being used.
    : int prime(int n);
    :
    : int main()
    : {

    Instead of 'math.h', include 'cmath', which is C++ standard.

    See ya,
    bilderbikkel
  • While it is perfectly legal C/C++ to write function prototypes and definitions in the same file, good programming practice is to do like this:

    [code]/* func.h */

    void func (void);[/code]

    [code]/* func.cpp */

    #include "func.h"

    void func (void)
    {
    cout << "hello world" << endl;
    }[/code]

    [code]/* main.cpp */

    #include "func.h"

    int main()
    {
    func();

    return 0;
    }[/code]


    In your example, you could for example make a "prime.h" with a corresponding "prime.cpp". Together they become a prime number module where all your prime number calculations go. Next time you need prime number functions, you can then simply take this source module and import it into your new project - since it is made completely independant of the rest of your code.
  • : While it is perfectly legal C/C++ to write function prototypes and
    : definitions in the same file, good programming practice is to do
    : like this:
    :
    : [code]: /* func.h */
    :
    : void func (void);[/code]:
    :
    : [code]: /* func.cpp */
    :
    : #include "func.h"
    :
    : void func (void)
    : {
    : cout << "hello world" << endl;
    : }[/code]:
    :
    : [code]: /* main.cpp */
    :
    : #include "func.h"
    :
    : int main()
    : {
    : func();
    :
    : return 0;
    : }[/code]:
    :
    :
    : In your example, you could for example make a "prime.h" with a
    : corresponding "prime.cpp". Together they become a prime number
    : module where all your prime number calculations go. Next time you
    : need prime number functions, you can then simply take this source
    : module and import it into your new project - since it is made
    : completely independant of the rest of your code.

    Thanks All
  • : : While it is perfectly legal C/C++ to write function prototypes and
    : : definitions in the same file, good programming practice is to do
    : : like this:
    : :
    : : [code]: : /* func.h */
    : :
    : : void func (void);[/code]: :
    : :
    : : [code]: : /* func.cpp */
    : :
    : : #include "func.h"
    : :
    : : void func (void)
    : : {
    : : cout << "hello world" << endl;
    : : }[/code]: :
    : :
    : : [code]: : /* main.cpp */
    : :
    : : #include "func.h"
    : :
    : : int main()
    : : {
    : : func();
    : :
    : : return 0;
    : : }[/code]: :
    : :
    : :
    : : In your example, you could for example make a "prime.h" with a
    : : corresponding "prime.cpp". Together they become a prime number
    : : module where all your prime number calculations go. Next time you
    : : need prime number functions, you can then simply take this source
    : : module and import it into your new project - since it is made
    : : completely independant of the rest of your code.
    :
    : Thanks Everyone
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories