Thread: function problem

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    5

    function problem

    have a problem, when int n=1 float a and b variable's are still the same when n=0. i write this program without function it work.

    Code:
    float people(float firstyear, float grow){
        float total;
        grow = firstyear / 100 * 4.5;
        firstyear= grow + firstyear;
        total=firstyear;
        return total;
    }
    
    int main()
    {
        float a = 65, b = 0;
        int n;
    
        for(n = 0; n < 5; n++){
        printf("%.2f\n", people(a ,b));
        }
        return 0;
    }
    without function
    Code:
    int main()
    {
        float firstyear=650000, grow=0;
        int n;
    
        for(n = 0; n < 5; n++){
    
        grow = firstyear / 100 * 4.5;
        firstyear= grow + firstyear;
    
        printf("%.2f\n", firstyear);
        }
        return 0;
    }
    Last edited by stablex; 09-02-2012 at 02:03 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In your first program you never change the values of a or b so the return value from the function will never change. In your second program you do actually change your variables each time thru your loop. If you run the program through your debugger, after putting in a breakpoint you should be able to single step through both programs and see the differences in your variables.


    Jim

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    The changes you've made to "grow" and "firstyear" inside your function are only visible within the function body (local scope). As soon as you return to main() all these changes are lost. Thus "a" and "b" in main() will never change.

    Bye, Andreas

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In other words,you need to pass a reference of these two variables in order the modifications made to them will be remember after the termination of the function

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    In your first program you never change the values of a or b so the return value from the function will never change. In your second program you do actually change your variables each time thru your loop. If you run the program through your debugger, after putting in a breakpoint you should be able to single step through both programs and see the differences in your variables.


    Jim
    yeah, how can i change float variable a and b during my for loops?

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Quote Originally Posted by std10093 View Post
    In other words,you need to pass a reference of these two variables in order the modifications made to them will be remember after the termination of the function
    pass a reference is using pointer?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by stablex View Post
    yeah, how can i change float variable a and b during my for loops?
    See the post i made 1 min ago

    Remember this

    Code:
    void modificationsRemain(int *a,int *b)
    {
              *a=500;/*random numbers*/
              *b=6000;
    }
    
    void modificationsNotRemain(int a ,int b)
    {
            a=5554543;
            b=5664;
    }
    
    int main(void)
    {   
             int a=5;
             int b=3;
             printf("a= %d and b= %d \n",a,b);
             modificationsNotRemain(a,b);
             printf("a= %d and b= %d \n",a,b);
             modificationsRemain(&a,&b);
             printf("a= %d and b= %d \n",a,b);
             return 0;
    }

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by stablex View Post
    pass a reference is using pointer?
    Exactly

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    but book exercise tell me need to use Call by Value. it that possible?
    Last edited by stablex; 09-02-2012 at 03:02 PM.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Yes it is possible, but you will need to return the value from your function to a variable that changes the variable you pass to your function. Also you really only need to pass one variable into your function.

    Jim

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    Yes it is possible, but you will need to return the value from your function to a variable that changes the variable you pass to your function. Also you really only need to pass one variable into your function.

    Jim
    do you have example code? because i dont really get it.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Here is one way of changing the value of a variable in main.

    Code:
    #include <stdio.h>
    
    int modifyInt(int value)
    {
       value += 10;
       return(value);
    }
    
    int main(void)
    {
       int number = 0;
       int i;
       for( i = 0; i < 5; ++i)
       {
          number = modifyInt(number);
          printf("The number is now: %d\n", number);
       }
    }
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  3. function inside function (most likely) problem
    By vlaskiz in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2011, 05:10 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM