Thread: Void Function Problem help

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    7

    Void Function Problem help

    I've got a problem with the bubble sort function:

    Code:
    void bubble_sort(long [],long);
    
    
    int main()
    {
      long array[100], n, c, d, swap;
    
    
      printf("Enter number of elements\n");
      scanf("%ld", &n);
    
    
      printf("Enter %ld integers\n", n);
    
    
      for (c = 0; c < n; c++)
        scanf("%ld", &array[c]);
    
    
      bubble_sort(array, n);
    
    
      printf("Sorted list :\n");
    
    
      for ( c = 0 ; c < n ; c++ )
         printf("%ld\n", array[c]);
    
    
      return 0;
    }
    
    
    void bubble_sort(long list[], long n)
    {
      long c, d, t;
    
    
      for (c = 0 ; c < ( n - 1 ); c++)
      {
        for (d = 0 ; d < n - c - 1; d++)
        {
          if (list[d] > list[d+1])
          {
    
    
            t         = list[d];
            list[d]   = list[d+1];
            list[d+1] = t;
          }
        }
      }
    }
    The problem is , if i delete the "void bubble_sort(long [],long);" line ,it shows up the "conflicting types for bubble_sort" and "implicit declaration of function bubble_sort" warnings .Can anyone explain why that line is necessary?(By the way i'm REALLY new to c so go easy on me...)

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    This happens because if the compiler doesn't know what the function prototype is, in C specifically, it creates an implicit prototype of the form:
    Code:
    int functionName();
    which later comes into conflict with your definition of the same function.

    Implicit declaration is an old rule and actually was declared invalid in C99, but many compilers still support it...
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Default99
    The problem is , if i delete the "void bubble_sort(long [],long);" line ,it shows up the "conflicting types for bubble_sort" and "implicit declaration of function bubble_sort" warnings .Can anyone explain why that line is necessary?(By the way i'm REALLY new to c so go easy on me...)
    At the point where you call the bubble_sort function in the main function, the bubble_sort function needs to be declared. This can be done by defining (implementing) the bubble_sort function before the main function, but instead of doing that, you can forward declare the bubble_sort function (e.g., what you did with that line) before the main function, then define the bubble_sort function after the main function.

    If you don't do this, then at the point bubble_sort is called in the main function, the compiler may assume that the bubble_sort function has int parameters (i.e., defaulting to int), hence the "conflicting types for bubble_sort" warning message.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it works perfectly fine for me just how it is posted, ( other than the two variables that are not needed at the time of posting, d, and swap) maybe you could also try to find a more updated compiler to install that is free on line and use that one instead of what you are now using.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by userxbw View Post
    it works perfectly fine for me just how it is posted,
    Their question was related to an issue when deleting an important line.

    Quote Originally Posted by Default99 View Post
    The problem is , if i delete the "void bubble_sort(long [],long);" line ,it shows up the "conflicting types for bubble_sort" and "implicit declaration of function bubble_sort" warnings .

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,632
    userxbw can't even understand the questions. Clearly English is not his first language (or if it is, that's pathetic). However, it seems to be just pure carelessness.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by john.c View Post
    userxbw can't even understand the questions. Clearly English is not his first language (or if it is, that's pathetic). However, it seems to be just pure carelessness.
    fart.... lets see here, he has his prototype to his function in the proper place. he has the definition in the write place. the function itself written out under main, and main, and it compiles and runs and does what it is suppose to do. without making any adjustments in my compiler. and you have a serious personalty issue that you defiantly need to look in to. Sorry your mommy didn't breast feed you enough or maybe she did. I do not know perhaps you need to look into that more too.

    as far as me not reading the entire question or missing that one part in there says nothing about your personality disorder does it?

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    7
    thanks for the anwers everybody I got it.The void function has to be before main and the warnings don't show up

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @userxbw and @john.c - please knock it off.

    @Default99 - The way you have it is preferable, but you can also define the function before main. See post #2 here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. control reaches end of non void function in bool function
    By Giwrgows x in forum C Programming
    Replies: 26
    Last Post: 12-19-2015, 02:58 PM
  2. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. void, void function
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 05:04 PM
  5. void non void function
    By modance in forum C Programming
    Replies: 6
    Last Post: 01-28-2003, 08:06 AM

Tags for this Thread