Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > return value from function became garbage

Reply
Thread Tools

return value from function became garbage

 
 
sandeep_agarwal sandeep_agarwal is offline
Junior Member
Join Date: Jul 2008
Posts: 1
 
      07-11-2008
hi,

I came across a funny problem today so thought of sharing with you guys.

I defined a function, something like this:

double calculateSomething(long a, long b, long c, long d)
{
double val = -1;
//do some calculation using a, b, c, d
//and store the return value in val
return val;
}

This function was called from fun1().

void fun1()
{
double amt = -1;
amt = calculateSomething(a, b, c, d); //The amt variable had proper value
}

There was another function also lets say fun2() with the same definition as
fun1 but defined above the calculateSomething function.

void fun2() //Defined above definition of fun2()
{
double amt = -1;
amt = calculateSomething(a, b, c, d); //The amt variable had improper value
}

Here the amt variable had some garbage value. I am using the gcc compiler and didnt do any forward declaration of the calculateSomething() function.

Here I expected that compiler should have given me an error but it gives a warning and takes an implicit declaration to return int.
The warning that it gives is
warning: `calculateSomething' was previously implicitly declared to return `int

I refer this problem as funny because it compiles properly and also while debugging I found that it goes in that function properly and calculates and returns the value correctly but after the value is returned in the local variable inside fun2 it becomes garbage.

This may seem like a trivial problem but it wasted some of my time as there are too many warnings in my program so I missed this one

The problem got solved after i put the declaration in the header file.
 

Last edited by sandeep_agarwal; 07-11-2008 at 10:01 PM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any function can convert the words form,such as "boys"->"boy","became->become"? ZSP747 C++ 5 12-25-2005 07:10 PM
Any function can convert the words form,such as "boys"->"boy","became->become". ZSP747 C++ 0 12-25-2005 01:11 PM
Any function can convert the words form,such as "boys"->"boy","became->become". ZSP747 C++ 0 12-25-2005 01:10 PM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments