Thread: Return Value

  1. #1
    Unregistered
    Guest

    Return Value

    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?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    Unregistered
    Guest
    Hi Magos,
    Is void a value?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Magos
    void

    However, I doubt that main() knows that the function is over due to the return 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.)

    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.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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:
    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;
    
    }
    and replace the letter in main() to test each one...

    ok, so that was just a chance for me to show how obfuscated i can be... any similar code should work

  6. #6
    Unregistered
    Guest
    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)?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    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
    Is void main ever correct (I've noticed that it is your pet peeve)?
    The reason it isn't correct is that the OS expects any program that runs to return a value to it.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Unregistered
    Guest
    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.

  9. #9
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    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.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally 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.
    Never use void main... Sorry, couldn't resist

    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:
    Code:
    int myAddFunctions( int a, int b ) { return a+b; }
    
    int main( void )
    {
        int x;
        x = myAddFunction( 2, 2 );
    
        return 0;
    }
    And 'x' would hold the value of 4 when it returns...

    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.

  11. #11
    Unregistered
    Guest
    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() ) ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM