Answers to questions on generating the value in a function

  1. Why is the return in the following function redundant?
    void out2(int v1, int v2)
    {
       printf("%d, %d", v1, v2);
       return;
    }
    
    The return comes immediately before the closing brace ('}') of the function's body and so there would be an implicit return at this point.
  2. Why is it not necessary to have an else before the second return in the following function?
    int greater(int v1, int v2)
    {
       if (v1> v2) return v1;
       return v2;
    }
    
    iIf the condition is true, the first return causes the function to terminate and so the second return can only happen if the first did not. Thus putting an else in makes no difference. You might like to do so for clarity, but a good compiler will generate exactly the same machine code.

    Back to the questions.


    Back to notes on generating return values.