# Thread: Function to reset variables

1. ## Function to reset variables

Hello,

I'm new to the board and programming and have a question that I hope someone could help me with. I have been learning about arithmetic assignment operators and was asked to write a program to calculate some values.

Code:
```#include <stdio.h>

main()
{
int x, y;

x = 1;
y = 3;
printf("x += y produces an answer of: %d.\n", x += y);

x = 1;
y = 3;
printf("x += -y produces an answer of: %d.\n", x += -y);

x = 1;
y = 3;
printf("x -= y produces an answer of: %d.\n", x -= y);

x = 1;
y = 3;
printf("x -= -y produces an answer of: %d.\n", x -= -y);

x = 1;
y = 3;
printf("x *= y produces an answer of: %d.\n", x *= y);

x = 1;
y = 3;
printf("x *= -y produces an answer of: %d.\n", x *= -y);

return 0;
}```
Upon inspecting my code, I thought it was rather inefficient to have to re-declare the variables before each printf statement to reset them. I thought it might be better to write a function that would reset the variables before each statement, but I'm not sure how to go about writing the code for the function. I was thinking along the lines of:

Code:
```int var_reset(int x, int y)
{
x = 1;
y = 3;
}```
but I don't know how to return the values back to main. I know this is elementary, and I probably just haven't gotten far enough in my studies to understand how to do this, but thought that someone here could lend a hand.

Thank you!

2. A function can only return one value. You will have to make use of pointers .

Code:
```Function call:
//Pass the address of the variables
var_reset ( &x, &y);

//Declare pointers to read thee values and modify the data
int var_reset(int *x, int *y)
{
*x = 1;
*y = 3;
return 0;
}```

3. Thanks for the reply dunxton!

I knew I should have waited to get further in my studies. Seems like I wanted to jump the "knowledge" gun

Thanks again!

4. Why do you have to use the += operator?
For example, if you change the first printf() statement to

Code:
`printf("x += y produces an answer of: %d.\n", x + y);`
You don't need to reset your variables. Or is it just the assignment to do it that way?

5. The assignment was to write a program that would print the result of each arithmetic assignment operator given (x +=y, x += -y, x -= y, x -= -y, x *= y, and x *= -y).

I first wrote the program without resetting x and y. When the first statement was calculated x += y, or x = x + y, the constant "4" was assigned to x so each successive statement was incorrect, that's when I learned I had to "reset" the variables before each statement was printed.

Looking at the code it just seemed redundant to have to reset them before each printf statement and that's what prompted my question. I will learn about pointers in the coming chapters, but curiosity ate at me and that's why I asked

6. Originally Posted by matrixx333
Code:
`main()`
as a side note - you need to read

FAQ > What's the difference between... > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

and start using proper main declaration

7. vart,

8. use pointers or use function inside function
Code:
```void func1()
{
int x,y;
void reset()
{
x=3;
y=4;
}
reset();
printf("x=%d,y=%d",x,y);
x=5;y=8;
printf("x=%d,y=%d",x,y);

reset();
printf("x=%d,y=%d",x,y);
}

int main()
{
func1();
return 0;
}```

9. Originally Posted by creeping death
use pointers or use function inside function
Code:
```void func1()
{
int x,y;
void reset()
{
x=3;
y=4;
}
reset();
printf("x=%d,y=%d",x,y);
x=5;y=8;
printf("x=%d,y=%d",x,y);

reset();
printf("x=%d,y=%d",x,y);
}

int main()
{
func1();
return 0;
}```
have you compiled it?

Code:
```test.c(5) : warning C4210: nonstandard extension used : function given file scope
test.c(5) : error C2143: syntax error : missing ';' before '{'
test.c(10) : warning C4013: 'printf' undefined; assuming extern returning int```

10. yes i have

Code:
```[c_d@localhost C]\$ gcc product.c
product.c: In function ‘func1’:
product.c:10: warning: incompatible implicit declaration of built-in function ‘printf’
[c_d@localhost C]\$ ./a.out
x=3,y=4x=5,y=8x=3,y=4[c_d@localhost C]\$```
the warning is because i did not include stdio.h

what compiler do you use?

11. vs2008
add some ompilation flags probably... like -WAll or -pedantic

I'm not too familiar with gcc configuration to make it ANSI compliant

12. Code:
```[c_d@localhost C]\$ gcc -Wall product.c
product.c: In function ‘func1’:
product.c:10: warning: implicit declaration of function ‘printf’
product.c:10: warning: incompatible implicit declaration of built-in function ‘printf’
[c_d@localhost C]\$```
Code:
```[c_d@localhost C]\$ gcc -pedantic product.c
product.c: In function ‘func1’:
product.c:4: warning: ISO C forbids nested functions
product.c:10: warning: incompatible implicit declaration of built-in function ‘printf’
[c_d@localhost C]\$ ./a.out
x=3,y=4x=5,y=8x=3,y=4[c_d@localhost C]\$```
Code:
```[c_d@localhost C]\$ gcc -ansi product.c
product.c: In function ‘func1’:
product.c:10: warning: incompatible implicit declaration of built-in function ‘printf’
[c_d@localhost C]\$```
Code:
```[c_d@localhost C]\$ gcc -std='c99' product.c
product.c: In function ‘func1’:
product.c:10: warning: implicit declaration of function ‘printf’
product.c:10: warning: incompatible implicit declaration of built-in function ‘printf’
[c_d@localhost C]\$```
i know about that warning...but is a way to get things done...like goto...and programmers have the right to know every option that they have...be it good or bad

and this isnt really bad...maybe awkward and kinda confusing...but not really bad...

surprising that MS doesnot allow nested functions...oO

13. Code:
```>gcc test.c -pedantic -Wall
test.c: In function `func1':
test.c:5: warning: ISO C forbids nested functions
test.c:10: warning: implicit declaration of function `printf'
test.c:22:2: warning: no newline at end of file```
warnings are warnings -you are using compiler extentions... on other compiler it will not compile

14. i think this is what you wanted

Code:
```[c_d@localhost C]\$ gcc -Werror -pedantic  product.c
cc1: warnings being treated as errors
product.c: In function ‘func1’:
product.c:4: error: ISO C forbids nested functions
product.c:10: error: incompatible implicit declaration of built-in function ‘printf’
[c_d@localhost C]\$```
yeah, sure original C did not have it because of the call stack issues...but nested functions comes in handy at times...

anyways...its upto the OP to see if it works on his compiler or not...

peace.

15. I also use gcc as my compiler. I compiled the code using:

Code:
`gcc -o doc1 doc1.c`
doc1.c being the name of the source code. I did not receive any errors or warnings. I then ran the machine code and everything worked.