Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Trying to understand return in C

  1. May 4, 2012 #1
    Trying to understand "return" in C

    Hello Forum,

    a function in the C language requires a "return" at the end...is that always true?

    what is exactly the purpose of the return? If there is no return, we can just write void or not write anything?

    I am familiar with matlab where there is no return.....why does C requires the specification of return instead?

    thanks,
    fisico30
     
  2. jcsd
  3. May 4, 2012 #2

    chiro

    User Avatar
    Science Advisor

    Re: Trying to understand "return" in C

    Hey fisico30

    The purpose of return is to return control of the program back to the routine that called your function. If your function is your entry point (like main), it will return the control back to the Operating System which will terminate your process.

    If it is a normal routine it will return control back to the routine that called your procedure.

    The reason why C requires this is because of a few main reasons.

    The first reason is that you are allowed to have multiple points of returning which means that if the user wants to return back to the code that called the function at any point, then they just put a return statement there and the compiler has to generate the code that the programmer needs to write.

    As an example of the above, you could put multiple returns within say if statements depending on the criteria of the if statements and some might return and others might say do something else (like call a function). By allowing the programmer to specify when they want to do things like this, it lets the programmer have the flexibility to do this and the compiler will generate the code that reflects this.

    The other reason is that when you are returning an actual object (like when your function is not void) then the compiler needs to make sure you are returning the right object type. The return keyword also includes an object type and the compiler will have to generate code that allocates space either on the stack or using a pointer to return the data specified in the statement.

    Basically it boils down to giving the programmer flexibility to tell the compiler specifically what they want done and not let the compiler guess it for them.

    Void statements will return at the end of the execution of the function, but again if you have say branching where you want different things to happen (like if a > 2 then run function y else return) then you can explicitly use this for a void function to do what you want.
     
  4. May 4, 2012 #3
    Re: Trying to understand "return" in C

    The main() function requires a return

    are

    return(void), return (0), return the same thing?
     
  5. May 4, 2012 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Re: Trying to understand "return" in C

    Not true.

    If a void function (a function that doesn't return anything) doesn't need a return statement. There is an implicit return statement at the brace that closes the function's definition. Omitting the return statement just before the closing brace is perfectly legal, and is the recommended in some places. Example:
    Code (Text):

    void update_object (ObjectType * object, double new_quantity) {
       object->quantity = new_quantity;
    }
     
    A return statement here is just useless clutter.

    Even in functions that do return some value, a return statement at the close brace isn't always needed. If the statement is unreachable, why bother? Example:
    Code (Text):

    int is_even (int number) {
       if ((number % 2) == 0) {
          return 1;
       }
       else {
          return 0;
       }
    }
     
     
  6. May 4, 2012 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Re: Trying to understand "return" in C

    C does not require this.
    A return statement at the end (which is what this thread is about) is not needed in a void function, nor is it needed in a value-returning function if the close brace is unreachable.
     
  7. May 4, 2012 #6

    Mark44

    Staff: Mentor

    Re: Trying to understand "return" in C

    I don't think you said exactly what you meant, since there isn't any unreachable code in the example above. Or I might be misunderstanding what you're saying. Maybe you had something like this in mind.

    Code (Text):

    int is_even (int number) {
       if ((number % 2) == 0) {
          return 1;
       }
       else {
          return 0;
       }
       return 7;
    }
     
    The third return statement is unreachable, so it is never executed.
     
  8. May 4, 2012 #7

    Mark44

    Staff: Mentor

    Re: Trying to understand "return" in C

    One thing not mentioned so far is a peculiarity of English. The verb "to return" is both transitive and intransitive. This means that it can be used with or without a direct object. That might be confusing to some, even native speakers of English, and possibly moreso to nonnative speakers

    A function can return (go back to the caller), and it can return (send back) a value.
     
  9. May 4, 2012 #8
    Re: Trying to understand "return" in C

    So, in general a function comprises, between its curly brackets, some instructions.

    A function can have its own input arguments, in between its parenthesis after its name....

    But a function can work with arguments that are not necessarily defined and declared inside the round brackets: for instance, in the example above,

    int is_even (int number)

    the name is is_even, and the input is the variable number...we could have declared the variable number before the function and used it inside the function is_even, correct?

    the return 0 and return 7 simply associate either value to the function because the output of the function is an integer....

    Many functions simply execute some instructions...I am familiar with Matlab, where return is not used and a function simply provides a set of instructions...

    thanks,
    fisico30
     
  10. May 4, 2012 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Re: Trying to understand "return" in C

    Matlab most certainly does have a return statement, and its behavior is exactly the same as C's.
     
  11. May 4, 2012 #10

    Mark44

    Staff: Mentor

    Re: Trying to understand "return" in C

    Sort of. The function definition consists of the type returned by the function, the function name, the list of formal parameters, and the body of the function (between the braces).

    You can also have a function declaration (or prototype), in which you tell the compiler the return type, the function name, and the number and types of the formal parameters.
    Yes.
    No. The formal parameters must be declared. In your example, number is the formal parameter, and its type is int.

    I might not be understanding your question. Are you talking about calling the function?
    No. If you declare number as an int inside the main function, the scope of that variable is just the main function. That means that the name number is not known outside main. If you declare number at the global level, you still have to declare the name of the formal parameter of your is_even function.

    If you have declared number as a global variable, and you declare it as a formal parameter of the is_even function, then what you have are two variables with the same name.
    That's an odd way to put it. The returned value is actually sent back to the caller.

    Code (Text):

    int fun(int); // function prototype

    int main(void)
    {
       int input = 3;
       int result;
       result = fun(input);
       return 0;
    }

    // function definition
    int fun(int value)
    {
       return 7;
    }
     
    In the code above, the value 3 is passed to fun, and fun immediately returns 7, which is then stored in result.
    Fortran distinguishes between routines that return a value (FUNCTION) and routines that cause something to happen (SUBROUTINE).

    In C and C++ functions either return some value or are void functions.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook