post/preincrementing function arguments

This is a discussion on post/preincrementing function arguments within the C++ Programming forums, part of the General Programming Boards category; I have a recursive function that accepts an integer value. Everytime the function is called the value passed is incremented. ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    167

    post/preincrementing function arguments

    I have a recursive function that accepts an integer value. Everytime the function is called the value passed is incremented. If I use the postincrement operator the value is not incremented but if I use preincrement it is. Why is this?
    silk.odyssey

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    Let me be the first the make a wild guess as to what the code looks like:
    foo(i++); vs foo(++i);
    The first one increments i then calls foo(i), the second one calls foo(i) then increments i.

    gg

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Everytime the function is called the value passed is incremented.
    Save yourself problems like this and don't actually change the value, just pass it added by one:
    Code:
    recursive_func ( val + 1 );
    This is even more important when you do something using val after the recursive call. If you didn't really want it incremented then you'll have issues:
    Code:
    #include <iostream>
    
    using namespace std;
    
    // Uncomment for correct operation and compare
    //#define CORRECT
    
    void tabme ( int val )
    {
      if ( val == 6 )
        return;
      for ( int i = 0; i < val; i++ )
        cout<<'\t';
      cout<<"In: "<< val <<'\n';
    #ifdef CORRECT
      tabme ( val + 1 );
    #else
      tabme ( ++val );
    #endif
      for ( int i = 0; i < val; i++ )
        cout<<'\t';
      cout<<"Out: "<< val <<'\n';
    }
    
    int main()
    {
      tabme ( 0 );
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    i am not sure I understand why val + 1 produces a different result than ++val.
    silk.odyssey

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    http://www.embedded.com/story/OEG20020429S0037
    http://www.embedded.com/showArticle....icleID=9900661

    EDIT:

    Ignore this post. There is nothing undefined about either of:
    Code:
    foo(i++);
    foo(++i);
    which is lucky, since we use both constructs frequently.

    Technically, it turns out that in both constructs i is incremented before the function call. However, i++ returns the value of i before it is incremented while ++i returns the value of i after it is incremented.

    It turns out there is a sequence point:
    Code:
    foo(sequence_point_here);
    which means that all arguments and side effects must happen before the function call.

    However, when two or more arguments are involved, there is no rule about order of evaluation.
    Last edited by anonytmouse; 04-10-2004 at 08:26 AM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i am not sure I understand why val + 1 produces a different result than ++val
    Because ++val changes val, val + 1 does not.
    My best code is written with the delete key.

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. Sending multiple arguments to function..
    By Zzaacchh in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2008, 03:20 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 01:15 PM
  4. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 02:26 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21