Thread: What's wrong with this function ?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    19

    What's wrong with this function ?

    Hello everyone

    i 'm a newbie in c and i have this exercise :

    --------------------------------------------------------------------------------
    What the error in the following function ?

    Code:
    void f(void)
    {
          int i ; 
          printf("n f() \n");
          /* calling f function 5 times */
          for (i=0;i<=5;i++)  f() ;
    }
    --------------------------------------------------------------------------------

    I can see no mistake . Can you ? It's kinf of urgent and any help would be deeeeeply appreciated


    Thanks a lot

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I agree there's no syntax error in the function. Logic errors, now, that's something else.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    tab thanks for replying.

    In order to see logical errors , there should be something asked like "this function is supposed to do "this" ..." , but there isn't .

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by johngav
    In order to see logical errors , there should be something asked like "this function is supposed to do "this" ..." , but there isn't
    It is in the comment: "calling f function 5 times". Presumably you get bonus marks if you take it a step further.
    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

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    EDIT: put my foot in my mouth there lol

    Hint: what is the value of i when the function enters the loop on the second time it is called.
    Last edited by KBriggs; 06-24-2009 at 11:03 AM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    KBriggs , no , it is an idefinat loop , i run it with dev++ and it prints n f() many many times till the execution window closes

    laser , i agree that the comment is the key , but what would be a step further?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by johngav
    laser , i agree that the comment is the key , but what would be a step further?
    What is the name of the function that is being defined? What is the name of the function that is being called?
    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

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    laser , f is the name in both cases , so it is a reccuring function , so ? Ican't get any further

    kbriggs the value is 1 isn't it ? , but that's not very helpful

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    The error is in the increment step. Consider:

    Code:
    void f(void)
    {
          int i ; 
          printf("n f() \n");
          /* calling f function 5 times */
          for (i=0;i<=5;i++)  f() ;
    }
    When is i incremented in that function? Before or after f() is called again? Which one should it be?

    Also, you are resetting i each time the function is called. I don't think that i can be local to a recursive function if you want it to hold increments between recursive calls.
    Code:
    int i = 0;
    void f(void)
    { 
    
         printf("n f() \n");
    
          /* calling f function 5 times */
          while (i <= 5)
          {
    	i++;
    	f();
          }
    }
    
    int main()
    {
         f();
    }
    Last edited by KBriggs; 06-24-2009 at 11:12 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by johngav
    f is the name in both cases , so it is a reccuring function , so ?
    So what is the base case for the recursion?
    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

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    kbriggs i guess it is incremented after f calling and that's probably the reason it is indefinate. but how should it be?

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    laserlight , do you mean why is recursion used in this case? I don;t know

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Try printing the value of i each time you call your function. You will see that it never gets incremented in your code because the next call to f() happens before the increment step, and it will repeat until the system segfaults.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by johngav
    do you mean why is recursion used in this case?
    No. I mean that there is no scenario in which the recursion will terminate, hence there is infinite recursion.

    As for the obvious problem that you appear to be missing. Let us assume that there is a function named g that takes no arguments, and that f and g are not mutually recursive. What is wrong with the implementation of this function f?
    Code:
    void f(void)
    {
          int i ; 
          printf("n f() \n");
          /* calling g function 5 times */
          for (i=0;i<=5;i++)  g() ;
    }
    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

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    I see your point Kbriggs .seems like a stupid exercise though no ? I mean how would this function not be wrong ? What would you change

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM