Thread: Incrementing a number each time a function is called.

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    Incrementing a number each time a function is called.

    Hi,

    I need to write a function so each time its called it will increment the number 1.

    Yes this is for homework, but Im trully stuck.

    See my problem is I cant use out algorithms and no input.


    I just cant imagine how to do this.

    I rewrote this post 100 times before posting it keep thinking I know what to do. But im lost in a sea of pointers and refrences and it just doesnt make sense anymore.

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    A simple static variable and a ++ operator should do it. Static variables don't clear from memory when the function loses scope.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Consider passing the variable you want incremented by reference to your function and then using ++ as Scribbler suggested.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Wow, thnx helped a lot. static for the win!

  5. #5
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Here's an example of the code Scribbler and Zach were talking about:
    Code:
    void NumPlus(int *n){
        cout<<n<<"\n";
        n++;
        }
    The static keyword means that when the function is called again the memory for n isn't erased.

    Here is the code for main.
    Code:
    int main(){
        int num;
        for(int i=0; i<10; i++)
        NumPlus(&num);
        }
    Last edited by fuh; 01-30-2005 at 06:26 PM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Almost what I was going for. I'd use a reference instead of a pointer.
    Code:
    void NumPlus(int& n)
    {
        cout<<n<<"\n";
        ++n; // No real difference in this case.
             // I just like ++n better than n++.
    }
    
    int main()
    {
        int num;
        for(int i=0; i<10; i++)
        NumPlus(num);
    }
    Also note that you have a bit of a bug:
    Code:
    void NumPlus(int *n)
    {
        cout<<n<<"\n";
        n++;
    }
    All you do is increment the pointer (which is not carried over). What you want is:
    Code:
    void NumPlus(int *n)
    {
        cout<<n<<"\n";
        (*n)++;
    }
    or
    Code:
    void NumPlus(int *n)
    {
        cout<<n<<"\n";
        ++*n;
    }
    *edit*
    I just realized that I completely misinterpreted your original question, so none of this is relevant anyway.
    Last edited by Zach L.; 01-30-2005 at 08:59 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    if you want to do it without screwing around with referencing just do this:

    Code:
    static int i;
    IntUp(i);
    
    void IntUp(int i)
    {
      i++
    }

  8. #8
    Registered User dug's Avatar
    Join Date
    Jun 2003
    Posts
    66
    isn't the i++ ambiguous?

    you'd be safer doing something like:

    Code:
    static int i;   //global!!
    IntUp(i);
    
    void IntUp(int d)
    {
        d++;
    }
    "take the long road.... and walk it."

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>isn't the i++ ambiguous?
    I believe 'i' is interpreted to be the 'i' defined at the innermost scope level, in this case the one passed as a parameter. Thus the parameter that is passed to the function is incremented, but the original remains unchanged.

    >>you'd be safer doing something like:
    It is prettier, I agree.

    As I interpret the question however, both of these methods are incorrect; the functions do absolutely nothing but waste CPU time (assuming they do nothing but increment the value passed as a parameter). If you do not pass by reference or pointer, then the original variable remains unchanged and nothing happens. If you want to avoid the reference/pointer, then you should pass no parameter and modify the global directly - but passing by reference/pointer is usually preferred, just because globals are often unsafe (you can accidentally access a global if you make a typo, for example, and it'll be hell to try and figure out where the bug's coming from).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    I believe the whole point of static variables is that you don't have to pass by reference and they do get changed if they're used in a function

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    static int i;   //global!!
    IntUp(i);
    
    void IntUp(int d)
    {
        d++;
    }
    In this case the static isn't really needed. static in this case does not change the lifespan nor the scope. All it does is make it have internal linkage only.

    See my problem is I cant use out algorithms and no input.
    Would you mind explaining this restriction some more.

    I believe the whole point of static variables is that you don't have to pass by reference and they do get changed if they're used in a function
    The whole point of static local variables is to have a variable that has the lifespan of the entire program but has a local scope. What you do with them is up to you.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>static local variables
    Yes, the key is local. In the example provided, the variable is declared globally and then passed by value as a parameter to IntUp(). So it exists as a global variable, and changes made to it persist - but if it is passed by value to IntUp(), then changes made to the parameter are not made to the static global you declared. Static is not just an alternate method of passing by reference.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    I didn't mean to declare it globally, I just didn't type int main() because it wasn't relevant to my example. what I meant was:

    Code:
    void IntUp (int i);
    
    int main()
    {
      static int i = 0;  //don't think the initialization is necessary 
                               //but what the hey
      IntUp(i);
      cout << i;
      return 0;
    }
    
    void IntUp(int i)
    {
      i++
    }
    that program works, try it. it initializes to 0 then it outputs 1, thus...it works

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Then you have a broken compiler.

    Edit: And heres why:
    Code:
    void IntUp (int i);
    
    int main()
    {
      static int i = 0;  //don't think the initialization is necessary 
                               //but what the hey
      IntUp(i);
      cout << i;
      return 0;
    }
    
    void IntUp(int i)
    {
      i++
    }
    1) When you call IntUp() a copy of i is made
    2) Missing semi-colon after i++
    3) not including iostream
    4) namespace
    Last edited by Thantos; 01-31-2005 at 02:00 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    hmm, you're right, it was working for me before...I must've screwed up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM