Thread: function declaration....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    function declaration....

    Normally when i use a function i make a declaration like this

    void func();

    So i am not passing any arguments to the function. But in some of the tutorials i noticed on the forum have the declaration like this

    void func(void);

    So till now i never used the void in my function declaration parentheses. But i was interested in knowing what does the compiler do when it comes across the above declaration in which i dont say void. Does it pass some garbage random values or the standard specifies that if a declaration doesnt specify anything it has to be assumed as void.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The function declaration
    Code:
    void func();
    means that the function func returns nothing and takes somewhere between zero and infinity parameters, all of type int. (EDIT: Not all of type int -- they can be of any type.) The function declaration
    Code:
    void func(void);
    means that the function func returns nothing and accepts no parameters.
    Last edited by tabstop; 08-15-2009 at 05:23 PM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So to elaborate, using (void) is a safety check -- it means if you try and call the function with arguments, the compiler will catch your mistake.

    Of course, it doesn't matter that much, since those arguments will be ignored anyway -- with () there is no way to access them. But if you were putting a library together, using (void) is probably a friendly thing to do.
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess I should be more specific. If the prototype is
    Code:
    void func();
    then the actual function itself could be
    Code:
    void func(int x) {}
    or
    Code:
    void func(double x) {}
    or
    Code:
    void func(int x, double y, char *z, struct tm a, int b, int c, float q) {}
    or even
    Code:
    void func() {}
    In this last case, where an empty set of parentheses is used when defining the function proper, then it means that there are no arguments to the function.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    But if i use this

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int func();
    
    int main(void)
    {
    
    	int i;
    	i = func(2, 4, 2.0);
    
    	printf("\n Func returned %d", i);
    
    	return 0;
    }
    
    int func()
    {
    	
    	return 1;
    }
    The compiler should throw an error but it doesnt.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    The compiler should throw an error but it doesnt.
    No, it will throw an error if
    Code:
    int func(void)
    Quote Originally Posted by tabstop View Post
    I guess I should be more specific. If the prototype is
    Code:
    void func();
    then the actual function itself could be
    Okay, that is how I do all my prototypes from now on then
    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

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by MK27 View Post
    No, it will throw an error if
    Code:
    int func(void)


    Okay, that is how I do all my prototypes from now on then
    No it simply generates a warning and runs even then

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int func(void);
    
    int main(void)
    {
    
    	int i;
    	i = func(2, 4, 2.0);
    
    	printf("\n Func returned %d", i);
    
    	return 0;
    }
    
    int func()
    {
    	
    	return 1;
    }

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    No it simply generates a warning and runs even then
    Not if you put (void) in the definition.
    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

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @roann
    This thread will be useful.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM