C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old February 25th, 2003, 08:44 AM
campbel8 campbel8 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 24 campbel8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 0
Return Value

Hello!

I have question regarding the return value of a function. I have two separate C files

In file1 I have:
double value = getValue();
printf("Value: %f\n", value);

In file2 I have a global defined as:
double v = 2;

Also in file2 I have a function defined as:
double getValue() {
  printf("V: %f\n", v);
   return v;
}

When this function gets called from file 1, it prints out the value correctly in getValue(), however, when I print out the return value, I get an incorrect value.

How can this be happening?

Thank-you

Reply With Quote
  #2  
Old February 25th, 2003, 11:36 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
these two pieces of code in seperate files:
Code:
/* file 1 */
#include <stdio.h>
#include "file2.c"

main()
{
	double value = getValue(); 
	printf("Value: %f\n", value);
}

Code:
/* file 2 */
#include <stdio.h>

double v = 2; 

double getValue()
{ 
	printf("V: %f\n", v); 
	return v;
}

result in:
Code:
V: 2.000000
Value: 2.000000

which is what you were expecting, i think.

could the problem be something to do with the include file 2 part? by the way, when i compiled these 2 files i only needed to specify file1.c in the compilation command, as file2.c is linked to and included within file1. h.t.h. if it doesn't help, you need to post what your actual wrong results were and a description the file setup i think.

Reply With Quote
  #3  
Old February 25th, 2003, 01:27 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
>> #include "file2.c"
This is valid, but bad programming practice. We've had quite a few threads about this topic here. Best way to handle it is to make a file2.h file to contain the prototype of the function in file2.c:
Code:
/* file2. h*/
double getValue();

/* file2.c */
#include <stdio.h>
#include "file2.h"
double v = 2; 

double getValue()
{ 
	printf("V: %f\n", v); 
	return v;
}

/* file1.c */
#include <stdio.h>
#include "file2.h"

int main(void)
{
	double value = getValue(); 
	printf("Value: %f\n", value);
	return 0;
}

Hope this helps!

Reply With Quote
  #4  
Old February 25th, 2003, 03:10 PM
campbel8 campbel8 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 24 campbel8 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 0
Thank-You

Thank-you,

The problem has been solved!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Return Value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway