Solved

Function not returning correct value.

Posted on 2004-10-07
9
1,150 Views
Last Modified: 2011-08-18
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
Comment
Question by:Shinru
9 Comments
 
LVL 23

Expert Comment

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

0
 

Author Comment

by:Shinru
ID: 12256299
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
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 300 total points
ID: 12256789
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
 
LVL 12

Expert Comment

by:stefan73
ID: 12256809
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:jinumjoy
ID: 12257892
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
 

Author Comment

by:Shinru
ID: 12258585
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
 

Author Comment

by:Shinru
ID: 12259038
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12259313
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
 

Author Comment

by:Shinru
ID: 12260547
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now