944,191 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1692
  • C RSS
Sep 18th, 2006
0

Regarding Function Returning a Value.

Expand Post »
I have a doubt on function returning a value. Consider the following program snippet.

  1.  
  2.  
  3. int add(int a,int b) {
  4. int c=a+b;
  5. return;
  6. }
  7.  
  8. int main() {
  9. int a=10,b=20;
  10. printf("%d %d %d",a,b,add(a,b));
  11. return(0);
  12. }

The above program gives the output as
10 20 30

But the function is not returning any value. According to the K&R C, Section 4.1,

".......there need be no expression after return; in that case, no value is returned to the caller."

I will be grateful if anyone explains the logic behind this cryptic.

Thanks in advance.
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006
Sep 18th, 2006
1

Re: Regarding Function Returning a Value.

Your code probably shouldn't compile. add(int, int) is specified to return an int, but doesn't, which should raise a compile time error.
Last edited by Infarction; Sep 18th, 2006 at 4:40 am.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 18th, 2006
1

Re: Regarding Function Returning a Value.

Compile your code in strict mode. I guess it should be error. Anyways return value in C on x86 is generally eax, where the result of the last calculation often happens to be placed. Maybe that's why you are seeing this result.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 18th, 2006
0

Re: Regarding Function Returning a Value.

Click to Expand / Collapse  Quote originally posted by Infarction ...
Your code probably shouldn't compile. add(int, int) is specified to return an int, but doesn't, which should raise a compile time error.
You are right. When I compiled in strict mode, it thrown a compilation error. But in normal mode it doesn't and it will act exactly as Grunt's explanation.
Anyway, thank you very much for your quick reply.
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006
Sep 18th, 2006
1

Re: Regarding Function Returning a Value.

Click to Expand / Collapse  Quote originally posted by Grunt ...
Compile your code in strict mode. I guess it should be error. Anyways return value in C on x86 is generally eax, where the result of the last calculation often happens to be placed. Maybe that's why you are seeing this result.
Cool explanation and I am cleared with all my doubts and even it has thrown an error when I compiled in strict mode as you expected.
Much appreciated reply
Reputation Points: 51
Solved Threads: 1
Light Poster
Iqbal_h_a is offline Offline
30 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: emulating seach engine using C
Next Thread in C Forum Timeline: need help in symbian coding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC