| | |
Regarding Function Returning a Value.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2006
Posts: 29
Reputation:
Solved Threads: 1
I have a doubt on function returning a value. Consider the following program snippet.
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.
C Syntax (Toggle Plain Text)
int add(int a,int b) { int c=a+b; return; } int main() { int a=10,b=20; printf("%d %d %d",a,b,add(a,b)); return(0); }
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.
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.
•
•
Join Date: Aug 2006
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
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. Anyway, thank you very much for your quick reply.
•
•
Join Date: Aug 2006
Posts: 29
Reputation:
Solved Threads: 1
•
•
•
•
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. Much appreciated reply
![]() |
Similar Threads
- passing data from one function to another in C (C)
- Exact meaning of functions returning reference (C++)
- Having trouble passing bool type to function....need help (C++)
- Help function not returning correct value (C++)
- How to declare this function? (C)
Other Threads in the C Forum
- Previous Thread: emulating seach engine using C
- Next Thread: need help in symbian coding
Views: 1461 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






