Storing result in a temp variable versus multiple return points

Posted by Carl Manaster on Stack Overflow See other posts from Stack Overflow or by Carl Manaster
Published on 2010-05-13T14:46:31Z Indexed on 2010/05/13 15:04 UTC
Read the original article Hit count: 211

Filed under:
|

Which is a better practice, generally speaking, and why? Under what circumstances would you change your mind?

function foo1(int x) {
  int result;
  if (x > 5) {
    result = 2;
  } else {
    result = 7;
  }
  return result;
}

OR

function foo2(int x) {
  if (x > 5) {
    return 2;
  } else {
    return 7;
  }
}

© Stack Overflow or respective owner

Related posts about subjective

Related posts about return-value