Thread: functions anywhere? (in the module)

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    functions anywhere? (in the module)

    Firstly, Hi to the board!

    Can anyone tell me what is wrong with this code? I have read that "A function can be defined anywhere in the module".
    Although when I define the same function before the main function, it works when I call it.
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    int main(int nNumberofArgs,
     char* pszArgs[])
    {
           void hello()
           {
                cout << "hello" << endl;
           }
           hello();
           system("PAUSE");
           return 0;
    }
    When I try to compile and execute I get the message...
    Line 9: expected primary-expression before "void".

    This works fine...
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    void hello(void)
           {
                cout << "hello" << endl;
           }
    int main(int nNumberofArgs,
     char* pszArgs[])
    {
           
           hello();
           system("PAUSE");
           return 0;
    }
    Any help would be greatly appreciated. Thanks!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think what is meant is "anywhere (but not inside another function)" which main() is a function.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    Talking

    Ah Thanks!
    So basically you define a function within another function, will keep that in mind.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can create your functions everywhere except inside other functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2009, 12:10 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  4. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 12:02 PM

Tags for this Thread