weird function output value problem

Discussion in 'C Programming' started by Michel Rouzic, Jun 17, 2005.

  1. I'm sure that it's a dumb problem where i surely did something dumb,
    but, I got a problem, I got a variable that we'll call var1. in the
    main() function, this variable var1 (of type unsigned) of a value of
    lets say 1000. now i got a function that we'll call func1, declared by
    void func1(unsigned var1) and called by func1(var1);. in this function
    at some point there is let's say var1=var1*2;. everything works fine
    until the end of that function, the problem is that as soon as i come
    back to the main function (after the execution of the func1 function)
    the value of var1 is not 2000 as expected but 1000

    i already encountered the same problem in other functions in the same
    program, so what did i do wrong?
     
    Michel Rouzic, Jun 17, 2005
    #1
    1. Advertising

  2. "Michel Rouzic" <> writes:

    >I'm sure that it's a dumb problem where i surely did something dumb,
    >but, I got a problem, I got a variable that we'll call var1. in the
    >main() function, this variable var1 (of type unsigned) of a value of
    >lets say 1000. now i got a function that we'll call func1, declared by
    >void func1(unsigned var1) and called by func1(var1);. in this function
    >at some point there is let's say var1=var1*2;. everything works fine
    >until the end of that function, the problem is that as soon as i come
    >back to the main function (after the execution of the func1 function)
    >the value of var1 is not 2000 as expected but 1000


    >i already encountered the same problem in other functions in the same
    >program, so what did i do wrong?



    Assuming that func1 appears *after* main,
    at your line: void func1(unsigned var1)
    rename 'var1' to 'fred' and notice that main's var1 now changes.

    Does that help you understand why?

    --
    Chris.
     
    Chris McDonald, Jun 17, 2005
    #2
    1. Advertising

  3. Chris McDonald <> writes:

    >"Michel Rouzic" <> writes:


    >>I'm sure that it's a dumb problem where i surely did something dumb,
    >>but, I got a problem, I got a variable that we'll call var1. in the
    >>main() function, this variable var1 (of type unsigned) of a value of
    >>lets say 1000. now i got a function that we'll call func1, declared by
    >>void func1(unsigned var1) and called by func1(var1);. in this function
    >>at some point there is let's say var1=var1*2;. everything works fine
    >>until the end of that function, the problem is that as soon as i come
    >>back to the main function (after the execution of the func1 function)
    >>the value of var1 is not 2000 as expected but 1000


    >>i already encountered the same problem in other functions in the same
    >>program, so what did i do wrong?



    >Assuming that func1 appears *after* main,
    >at your line: void func1(unsigned var1)
    >rename 'var1' to 'fred' and notice that main's var1 now changes.


    Doh! Forget that suggestion, sorry.

    Rename all instances of 'var1' inside func1() to 'fred',
    and now notice that main's 'var1' still does not change.

    --
    Chris.
     
    Chris McDonald, Jun 17, 2005
    #3
  4. "Michel Rouzic" <> writes:
    > I'm sure that it's a dumb problem where i surely did something dumb,
    > but, I got a problem, I got a variable that we'll call var1. in the
    > main() function, this variable var1 (of type unsigned) of a value of
    > lets say 1000. now i got a function that we'll call func1, declared by
    > void func1(unsigned var1) and called by func1(var1);. in this function
    > at some point there is let's say var1=var1*2;. everything works fine
    > until the end of that function, the problem is that as soon as i come
    > back to the main function (after the execution of the func1 function)
    > the value of var1 is not 2000 as expected but 1000
    >
    > i already encountered the same problem in other functions in the same
    > program, so what did i do wrong?


    You assumed that arguments are passed by reference rather than by
    value.

    For example:

    void func(unsigned int param)
    {
    param = 2000;
    }

    int main(void)
    {
    unsigned int var = 1000;
    func(var);
    return 0;
    }

    The call to func() passes a copy of the value of var, not a reference
    to the variable itself. Inside func, "param" is just a local
    variable; any changes in its value won't affect var.

    If you want to change the value, you'll need to pass a pointer:

    void func(unsigned int *param)
    {
    *param = 2000;
    }

    int main(void)
    unsigned int var = 1000;
    func(&var);
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jun 17, 2005
    #4
  5. Chris McDonald wrote:
    > Chris McDonald <> writes:
    >
    >
    > >Assuming that func1 appears *after* main,
    > >at your line: void func1(unsigned var1)
    > >rename 'var1' to 'fred' and notice that main's var1 now changes.

    >
    > Doh! Forget that suggestion, sorry.
    >
    > Rename all instances of 'var1' inside func1() to 'fred',
    > and now notice that main's 'var1' still does not change.
    >
    > --
    > Chris.


    um... yeah i know that inside my function i could change it to
    anything, i just like to keep the same names for the same variables in
    and outside of a function, but um.. the point is to return a changed
    value for var1 (or fred if i declare void func1(unsigned fred) and do
    fred=fred*2; and call func1(var1);) and well do you know how to fix
    that?

    > >Assuming that func1 appears *after* main,


    um.... i write my func1 function before main... like

    void func1(unsigned var1) //or fred if you prefer
    {
    var1=var1*2;
    }
    int main()
    {
    unsigned var1;
    var1=1000;
    func1(var1);
    } //and there var1 is still 1000
     
    Michel Rouzic, Jun 17, 2005
    #5
  6. > You assumed that arguments are passed by reference rather than by
    > value.


    oooh, i didnt know that. so... the only way to output of a function
    something that was at its input is a pointer?
     
    Michel Rouzic, Jun 17, 2005
    #6
  7. Michel Rouzic wrote:
    > I'm sure that it's a dumb problem where i surely did something dumb,
    > but, I got a problem, I got a variable that we'll call var1. in the
    > main() function, this variable var1 (of type unsigned) of a value of
    > lets say 1000. now i got a function that we'll call func1, declared by
    > void func1(unsigned var1) and called by func1(var1);. in this function
    > at some point there is let's say var1=var1*2;. everything works fine
    > until the end of that function, the problem is that as soon as i come
    > back to the main function (after the execution of the func1 function)
    > the value of var1 is not 2000 as expected but 1000


    Your expectations are wrong
    This code:
    void f(unsigned v1) { v1 *= 2; }
    int main(void) {
    unsigned v1;
    f(v1);
    return 0;
    }
    means exactly the same thing as
    void f(unsigned crap) { crap *= 2; }
    int main(void) {
    unsigned v1;
    f(v1);
    return 0;
    }

    There are two places that you could be confused.
    1) The name of the formal parameter in the definition of f() has *no*
    relationship to any instance of that variable name outside the
    function.
    2) The function's formal parameter is initialized with a copy of
    the value of the argument with which it is called. It does not
    operate on the argument in the calling function.
    If you find that this is unclear and neither your text nor your teacher
    can make it clearer, someone here -- perhaps even I -- will surely be
    glad to help if you can tell us what confusion remains.
     
    Martin Ambuhl, Jun 17, 2005
    #7
  8. Michel Rouzic

    pete Guest

    Michel Rouzic wrote:

    > void func1(unsigned var1) //or fred if you prefer
    > {
    > var1=var1*2;
    > }
    > int main()
    > {
    > unsigned var1;
    > var1=1000;
    > func1(var1);
    > } //and there var1 is still 1000


    /* BEGIN new.c */

    #include <stdio.h>

    void func1(unsigned var1)
    {
    var1 *= 2;
    }

    void func2(unsigned *var1)
    {
    *var1 *= 2;
    }

    int main(void)
    {
    unsigned var1;

    var1 = 1000;
    func1(var1);
    printf("var1 is %u\n", var1);
    func2(&var1);
    printf("var1 is %u\n", var1);
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, Jun 17, 2005
    #8
  9. On Fri, 17 Jun 2005 01:37:29 -0700, Michel Rouzic wrote:

    >> You assumed that arguments are passed by reference rather than by
    >> value.

    >
    > oooh, i didnt know that. so... the only way to output of a function
    > something that was at its input is a pointer?


    Functions have return values but if you need to return more then one thing
    then, yes, you would typically use pointer arguments.

    Function arguments are ALWAYS passed by value in C. Something that may
    appear to break this rule at first sight but in fact doesn't is arrays. C
    doesn't have an array "value" as such, when you try to take the value of
    an arry you get a pointer to its first element which is why things like

    char array[10];
    char *ptr = array;

    work. The same happens when you try to pass an array as an argument.
    given

    void foo(char *ptr)
    {
    strcpy(ptr, "FOO");
    }

    I can write

    foo(array);

    and the string FOO will be copied into my local array. That's possible
    because a pointer is being passed (by value) which allows the function
    foo() to access what it points at, i.e. the array in the caller. As the
    example shows this is important for library function like strcpy() too.

    Lawrence
     
    Lawrence Kirby, Jun 17, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme
    Replies:
    1
    Views:
    623
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    587
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    618
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    440
    Captain Paralytic
    Jan 21, 2011
  5. will
    Replies:
    6
    Views:
    409
    Phrogz
    Dec 27, 2006
Loading...

Share This Page