Thread: Function to reset variables

  1. #1
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67

    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. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    19
    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. #3
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    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. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    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. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matrixx333 View Post
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    vart,

    I apologize for my ignorance. Thank you for the link. I will adhere to your advice.

  8. #8
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    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. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creeping death View Post
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    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. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    vs2008
    add some ompilation flags probably... like -WAll or -pedantic

    I'm not too familiar with gcc configuration to make it ANSI compliant
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    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
    Last edited by creeping death; 03-20-2009 at 08:45 AM. Reason: nested functions...not nested loops

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    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.
    Last edited by creeping death; 03-20-2009 at 09:20 AM.

  15. #15
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM