I've written some proof of concept programs and have some posts on this subject with regard to the x86 architecture and how values are passed back, and related to it, why it's a bad idea to declare main() as returning void.
For the x86 architecture, the CPU has physical storage places for computation and data manipulation called registers. Various registers serve various purposes. Most operations cannot be performed directly on values in computer memory, and these values must be moved from RAM to a CPU register. C hides a lot of this aspect of computer programs because C was meant to be more portable than dealing with assembly language and the machine architecture. It's pretty fairly certain you'll have RAM of some sort in your program no matter what architecture. It's not certain at all what registers you may or may not have and how you can use them. Therefore, C sticks to dealing with variables as if they are in computer memory all the time and the aspect of moving them around and getting them in the right positions inside the CPU and back again is left to the compiler.
Still sticking with the x86 architecture, there are four general-purpose registers that support math operations. They are named EAX, EBX, ECX, and EDX. The general way function values are returned is by writing the return value into the EAX register at the end of the function. The calling function then reads in that value.
Now here's an example of what could happen should this code actually work:
Code:
int myFunc()
{
printf("Bleh\n");
// Note that there is no return statement..
}
...
int x = myFunc();
Should this actually compile, x should contain the number 5. Why? Because printf() returns an int. printf()'s return value is the number of chars printed. In this case it would be, 'B', 'l', 'e', 'h', and '\n'. Since myFunc() doesn't do anything else the return value of 5 is left in the EAX register. myFunc() leaves EAX alone and just returns leaving what was in there previously. When control returns back from myFunc(), the value in EAX is assumed to be the return value, and x is assigned the number 5.
Proof of concept (which may or may not work for you):
Code:
#include <stdio.h>
int myFunc(void);
int main(void)
{
int x = myFunc();
printf("x = %d\n", x);
return 0;
}
int myFunc(void)
{
printf("Bleh\n");
}
Compiling with MinGW on an x86 system, I received the following output:
Now with that said, do not try to make assumptions about return values. Always explicitly return values, even in the case of C99 and C++ that supports leaving the return main() (because it does it automatically), if you'd like to be proactive in catching errors (although those cases it's not necessary). It's best to always make sure your program is well defined. Once you get into the area of undefined behavior..... you should not expect your program to work.