?
Solved

function not returning expected value ?

Posted on 2005-04-07
6
Medium Priority
?
1,412 Views
Last Modified: 2011-09-20
I have the following code:


#include <stdio.h>

int get(int);

int main()
{
int testvalue = 0;

printf("the value is %d", testvalue);

add (testvalue);

printf("the value is %d", testvalue);
}

int add(testvalue)
{
testvalue++;
return testvalue;
}

This does’nt run as I would expect:

This is how I imagined it would run:

1.      prints – the value is 0
2.      runs the function ‘add’
3.      prints – the value is 1

Instead it prints 0 each time.

I imagine that this is something to do with with returning variables in an incoorect manner.

Can anybody explain whats going on here, and also how I can return and overwite a variable which is declared in main() and then changed in a function, and then the new value returned into main ?
0
Comment
Question by:andyw27
  • 4
6 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13727813
Hi andyw27,
> >add (testvalue);
Try changing that to the following:
testvalue = add (testvalue);


David Maisonave :-)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 13727835
Your add function is getting a copy of the calling function's variable, so it can not modify the calling functions data.
You can either use the method I posted, or you can change your function to take a pointer to the variable.
int add (int*);

You can then change the calling function's variable.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13727853
Example:
int add(int *testvalue)
{
(*testvalue)++;
return testvalue;
}

int main()
{
int testvalue = 0;

printf("the value is %d", testvalue);

add (&testvalue); //**Change this to past a pointer to the variable

printf("the value is %d", testvalue);
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:overlook
ID: 13728095
Because the data operated in your add() function is just a copy of your original data.

Ather modification can use the reference instead of pointer
void add( int& testvalue)
{
    testvalue++;
}



main()
{
...

add(testvalue);
...

}
0
 
LVL 30

Expert Comment

by:Axter
ID: 13728122
>>Ather modification can use the reference instead of pointer
>>void add( int& testvalue)

You can do this in C++, but not in C.
C does not have reference variables.

For C, you need to use pointers, or return back by value as in both examples I posted.
0
 

Author Comment

by:andyw27
ID: 13731023

Thank you
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Suggested Courses

839 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