Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1163
  • Last Modified:

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.
0
Shinru
Asked:
Shinru
1 Solution
 
brettmjohnsonCommented:
There is nothing wrong with fncn().  You will have to post the code that
calls fncn (specifically the code dealing with x).

0
 
ShinruAuthor Commented:
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.
0
 
PaulCaswellCommented:
Shinru,

You need to use:

valid_date = chk_date(user_date);

There are other problems with this code but the essential problem you have here is that when a value is passed into a function as a parameter, that value is a COPY of the original. If you want the original to change you either must return a new value for it or pass a pointer to the original.

Paul
 
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
stefan73Commented:
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
0
 
jinumjoyCommented:
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
0
 
ShinruAuthor Commented:
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.
0
 
ShinruAuthor Commented:
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.
0
 
PaulCaswellCommented:
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
0
 
ShinruAuthor Commented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now