Link to home
Start Free TrialLog in
Avatar of Shinru
Shinru

asked on

Function not returning correct value.

Hello there, I've got a problem where a function won't return the correct value. I'm positive it's passing the variable in fine, though if I manipulate that variable in the function, it doesn't return correctly. For a quick example, if I have a variable int x, and I passed it into the function fncn(), and the function is:

int fncn(int y){
    y++;
    return y;
}

If I try to print the new value that the function got for x, it will only print the original value that was stored in x. I hope I made this clear enough for everyone, if not, just ask. Thanks everyone.
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

There is nothing wrong with fncn().  You will have to post the code that
calls fncn (specifically the code dealing with x).

Avatar of Shinru
Shinru

ASKER

Alright, the actual code is going to be a little different from the example. Also, the background for this is that I'll have the user put in a date. Then I want to pass that date into a function that will test it to make sure it is a valid date (it can't be a day previous to a date that has already been entered by the user, or above 31). The third variable is simply used in a while loop, to tell the loop whether to loop again if the date is invalid, or to continue on if the date is valid. The function call is shown here:

//Preceding code here
while(valid_date == 1){
                        printf("Please enter a valid date from %d to 31.\n", date);
                        scanf("%d", &user_date);
                        chk_date(user_date, valid_date, date);
                  }

//The continuing code here

The function body is shown here:

int chk_date(int usr_date, int vld_date, int fncn_date)
{
      if(vld_date < fncn_date || vld_date > 31){
            ;
            return 0;
      }
      else{
            vld_date = 0;
            fncn_date = usr_date;
            return vld_date, fncn_date;
      }
      
}

As you can see, when the date is valid, the function should set the valid date to 0, making the while statement false and thus dropping down to the continuing code. But the problem is that the function isn't returning that variable with the change, even though in the function it does change it. I sort of monitored the progress of the variable at specific points to see if it was a problem with the passing, changing, etc, and the only thing that I can see is that it isn't returning properly. I haven't had a problem with any of my other functions doing this, either. Hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Shinru,
What you did is passing by value. You need to pass by reference:

int fncn(int* y){
    (*y)++;
    return *y;
}

...and call your function with
fncn(&x);

Call-by-value is a "feature" of C. In C++, you could simply do:

int fncn(int& y){
    y++;
    return y;
}

...and everything would work as you expected.

Cheers!

Stefan
Hi,

I guess u have to take up what Paul is suggesting.

Try to understand the difference between Pass by reference and Pass by value first. What you are doing is pass by value.
Let me just complete ur eaxmple.

int fncn(int y){
    y++;
    return y;
}

main()
{
   x = 10;
   x = fncn(x);
   printf("new value of x is %d\n", x);
}

If you dont want to do x = fncn(x); then you will have to pass x by reference i.e fncn(&x);

hope that helps..

-Jinu
Avatar of Shinru

ASKER

Ahh, yes, I know exactly what you're talking about. It didn't occur to me though that I was doing that. Thank you for pinpointing that.
Avatar of Shinru

ASKER

You said there were other problems with the code (as I expected), do you think you could go in depth on those, just so I have an idea of some things I'm doing wrong and can correct those practices. I'd be very appreciative! Thanks again.
Shinru,

We have to be careful with homework questions. We are not allowed to give you code, we can only advise you on how to solve the problems you have. If you could post your current code and tell us what is going wrong, maybe we could take a look.

You may get a better response if you post it as another question as not many people will wish to take time to answer if there are no points involved.

Paul
Avatar of Shinru

ASKER

I understand, and I'm not really looking for code, I was just curious as to what faults you saw in what I gave, since you said there were a few. I just don't want to keep doing the same thing if it's not good practice to do so. Just something if you want to comment on, I'm not really looking for anything in particular, so I don't really mind if you do, don't really mind if you don't.