# Incrementing a number each time a function is called.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-30-2005
Coder87C
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.
• 01-30-2005
Scribbler
A simple static variable and a ++ operator should do it. Static variables don't clear from memory when the function loses scope.
• 01-30-2005
Zach L.
Consider passing the variable you want incremented by reference to your function and then using ++ as Scribbler suggested.

Cheers
• 01-30-2005
Coder87C
Wow, thnx helped a lot. static for the win!
• 01-30-2005
fuh
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);     }```
• 01-30-2005
Zach L.
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. :rolleyes:
• 01-31-2005
Strait
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++ }```
• 01-31-2005
dug
isn't the i++ ambiguous?

you'd be safer doing something like:

Code:

```static int i;  //global!! IntUp(i); void IntUp(int d) {     d++; }```
• 01-31-2005
Hunter2
>>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).
• 01-31-2005
Strait
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
• 01-31-2005
Thantos
Quote:

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.

Quote:

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

Quote:

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.
• 01-31-2005
Hunter2
>>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.
• 01-31-2005
Strait
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
• 01-31-2005
Thantos
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
• 01-31-2005
Strait
hmm, you're right, it was working for me before...I must've screwed up
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last