Skip to main content
gamedev.net
🔒 Locked

implicit return values?

Started by rootbin Jul 8, 2004 at 4:24 PM 6 replies 500+ views
Original Post
rootbin
rootbin
can anyone tell me what a nonvoid function returns if no return value is given? when using gcc, this gives a warning, not an error. does it return garbage? does it ALWAYS return zero? is it the language standard or compiler specific?
Telastyn
Telastyn
I'd wager it is compiler specific. I don't know for sure though, as it's easy enough to make sure the function returns something.

[edit: while I cannot find anything definative quickly, the side-comments I did find imply that it is compiler specific, and will tend towards returning garbage]
rootbin
rootbin
well, if you write a small program with function 'int func() { }' and print the result, you will get zero. however, it could be that the function is just throwing me whatever is on the stack at any given time, which happens to be zero because of the size of the program.
pjcast
pjcast
The return value on x86's is placed in the eax register not on the stack. However, you are right that you will just get random data. Perhaps eax is empty, perhaps there's something in it. Unless you write the assembly yourself, you have no idea what the compiler has used for registers.

Jingo
Jingo
The main() function has an implicit return 0; result.
rootbin
rootbin
so are you telling me that it is hardware/platform specific as well?
pjcast
pjcast
Could very well be different on Macs... And I'm pretty positive it's different on mainframes.

But one should never rely on quirks of a compiler. If you method/function is defined to return a value. For heavens sake just return a value. Espeacial if you are going to check it later, else why would you define the function to have a return value.
Sneftel
Sneftel
The behavior is undefined. That means that it is not necessarily architecture-specific or compiler-specific. It could return 0. It could return 37. It could cause your computer to burst into flames and order a pizza. It could do something different each time you compile it, or each time you run it. Don't count on the behavior under any circumstances.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.