Regarding Function Returning a Value.

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Regarding Function Returning a Value.

 
0
  #1
Sep 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Regarding Function Returning a Value.

 
1
  #2
Sep 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Regarding Function Returning a Value.

 
1
  #3
Sep 18th, 2006
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.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: Regarding Function Returning a Value.

 
0
  #4
Sep 18th, 2006
Originally Posted by Infarction View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 29
Reputation: Iqbal_h_a is an unknown quantity at this point 
Solved Threads: 1
Iqbal_h_a Iqbal_h_a is offline Offline
Light Poster

Re: Regarding Function Returning a Value.

 
1
  #5
Sep 18th, 2006
Originally Posted by Grunt View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1461 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC