If you don't ask a function to return a certain value, what value does it return to let main know that that the function is finished?
If you don't ask a function to return a certain value, what value does it return to let main know that that the function is finished?
void
However, I doubt that main() knows that the function is over due to the return value...
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Hi Magos,
Is void a value?
Think of your program as an actor reading from a script. How C differs is that instead of simply reading line by line, there are jumps that take you all over the book. (Similar to a 'choose your own adventure' book, if you know what those are.)Originally posted by Magos
void
However, I doubt that main() knows that the function is over due to the return value...
What happens is this:
this line happens
this line happens
this line is a function call that says "turn to page 10 in the book"
each line in that function happen, line by line
when the function has no more lines, they get a "return to where you were" message
You turn back to the page you were on, and continue from there.
Quzah.
Hope is the first step on the road to disappointment.
i once thought that if the function returns an int usually (i think) it returns zero by default. i thought void was a lack of any return value...
when a nasty bug crept up i was soon proven wrong. if you're unsure, always be explicit.
but if you want to prove us all wrong (or right) try this:
and replace the letter in main() to test each one...Code:#include <stdio.h> int main() { printf("%c",isvowel('a') ? 'y' : 'n'); } int isvowel (char x) { //obfuscated if ((toupper(x)==79 || toupper(x)==85 || ((x-1)%4==0 && (toupper(x)-65)/4 >=0 && (toupper(x)-65)/4 <=2 )) ?1:0) return 1; }
ok, so that was just a chance for me to show how obfuscated i can be... any similar code should work
Hi Quzah,
I guess what I'm wondering is: does the function send back a number to signal that it has completed, and it allows us to specify that number, or is the return value something optional that is added only when we require it.
Also, I'm reading the 1997 edition of C in 21 Days, and aside from global variables all over the place, it sometimes uses void main. Is void main ever correct (I've noticed that it is your pet peeve)?
Actually, everything is pushed onto the stack, and popped off a line at a time. Thus, when you get to the end of the function, it pops the last item off and returns you back to where you were in the previous function.Originally posted by Unregistered
Hi Quzah,
I guess what I'm wondering is: does the function send back a number to signal that it has completed, and it allows us to specify that number, or is the return value something optional that is added only when we require it.
The reason it isn't correct is that the OS expects any program that runs to return a value to it.Originally posted by Unregistered
Is void main ever correct (I've noticed that it is your pet peeve)?
Quzah.
Hope is the first step on the road to disappointment.
Hi Quzah,
I rechecked and was wrong about C in 21 Days using void main. I think what happened was that I saw so many void functions that, after awhile, I assumed void main. Your explanation about the OS makes sense.
So, am I right in thinking that
a) Any function defined as "void function()" does not return anything? Of course main is always an int so it can return a value to the OS.
b) Any function could be defined as int and used to return a value to the calling function, which would then give the calling function some information about how the function it called ran?
Or does "void function()" return an undefined value to the calling function? I'm sure I've seen it said.
a) void main() does not return a value to the OS
b) void main() returns an undefined value to the OS
Which of these is correct, or are both correct in certain circumstances.
PS. Please don't reply with "Never use void main" because that's a given.
Never use void main... Sorry, couldn't resistOriginally posted by Azuth
So, am I right in thinking that
a) Any function defined as "void function()" does not return anything? Of course main is always an int so it can return a value to the OS.
b) Any function could be defined as int and used to return a value to the calling function, which would then give the calling function some information about how the function it called ran?
Or does "void function()" return an undefined value to the calling function? I'm sure I've seen it said.
a) void main() does not return a value to the OS
b) void main() returns an undefined value to the OS
Which of these is correct, or are both correct in certain circumstances.
PS. Please don't reply with "Never use void main" because that's a given.
a) True. A void function returns nothing when it returns.
b) True & False. True any function can return an int (assuming it's some function you've written); False, the return value has nothing to do with letting the program know specificly when it exits.
When a function exits, control is automaticly returned to whatever function calls it.
The purpose of actually returning something is so that you can pass a value back to the function that calls it, so that it can do something with it.
For example: if we wanted to make a function return the addition of two passed arguments, we may do:
And 'x' would hold the value of 4 when it returns...Code:int myAddFunctions( int a, int b ) { return a+b; } int main( void ) { int x; x = myAddFunction( 2, 2 ); return 0; }
A 'void' function doesn't return anything. This is different than a function that returns a void pointer.
main is always supposed to return an integer. Making it return anything else is "undefined behaviour" which may cause problems, or may do nothing at all, depending on your OS.
Quzah.
Hope is the first step on the road to disappointment.
All functions do return a value. For example, X86's function return value in register AX. Any function complete and return to caller, the AX register contain a value. If your function is defined as
int yourFunct() ...
you must return an integer when your function complete. and this value is put in AX ( in machine code ).
if your function is
void yourFunc() ....
you do not return any value, but AX still there and contain some garbage the caller can not rely on! ( Especially if you write assembly to call yourFunc() ) ....