Thread: A strange function definition! is it legal???

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    A strange function definition! is it legal???

    Code:
    /*nothing here*/getsum(i)
    int i;  /*what's this???*/
    {
        /*... ...*/
    }
    is it legal??? is it in the standard???
    Last edited by Antigloss; 08-31-2005 at 01:00 AM.

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    The following code is strange for me. I don't specify a return value to function getsum, but it turns out that sum is actually returned to function main. why?

    Code:
    #include <stdio.h>
    
    getsum(i)
    int i;
    {
    	int sum = i;
    	return sum;
    }
    
    int main( void )
    {
    	printf( "%d", getsum(10) );
    
    	return 0;
    }

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    That style is equivalent to:
    Code:
    getsum(int i)
    {
        int sum = i;
        return sum;
    }
    It was used by K&R originally, before ANSI C.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by cwr
    That style is equivalent to:
    Code:
    getsum(int i)
    {
        int sum = i;
        return sum;
    }
    It was used by K&R originally, before ANSI C.
    But how can it return a value since we don't specify one?

    And what are the benifits for using
    Code:
    getsum(i)
    int i
    {
        int sum = i;
        return sum;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But how can it return a value since we don't specify one?
    If anything is missing, K&R 'C' assumed int
    So you have in effect 'int getsum( int i );

    > And what are the benifits for using
    None at all - ANSI-C prototypes make for a much better life.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by Antigloss
    The following code is strange for me. I don't specify a return value to function getsum, but it turns out that sum is actually returned to function main. why?

    Code:
    #include <stdio.h>
    
    getsum(i)
    int i;
    {
    	int sum = i;
    	return sum;
    }
    
    int main( void )
    {
    	printf( "%d", getsum(10) );
    
    	return 0;
    }

    Wow it compiled That kind of style is confusing i wont use that.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yes, sorry, I missed the implicit int return confusion. I still used to older style code omitting the int that my brain immediately glossed past it as quite normal.

    And, indeed, it's a much better idea to always have an explicit return type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. strange segmentation fault
    By Lateralus in forum C Programming
    Replies: 2
    Last Post: 06-10-2005, 09:19 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM