Home > database >  How to make an increment function in C?
How to make an increment function in C?

Time:08-18

I am trying to make a function in which you take in a value and it returns the value increased by 1.

For example,

int n=5;
printf("%d \n", n);
increment(n);
printf("%d \n", n);

should give an OUTPUT of:

5     
6

Changing the value by using n or n =1 inside the increment function is not changing the global variable. I do realise that pointers can be used to solve this problem, but I am unable to figure out how. Please help me out.

CodePudding user response:

If you want to see the result that value n changed, you can do it without using pointers

int n = 5;
printf("%d \n", n);
n = increment(n);
printf("%d \n", n);

And your increment function must return value with type int

CodePudding user response:

you can use this function

 void increment(int &n)
 {
    n = n   1;
 }

this works for me.

  • Related