0

I would like to use a function to check if a condition is true. And I saw this illustration in somewhere:

function check_condition() {
   if( first_condition ) {
      return false;
   }

   return true;
}

What I don't understand is that even if the first condition returns false, the function eventually still returns true because the condition inside the function is above the return true?

2
  • The first encountered return breaks out of the function. Nothing after is evaluated. Commented Jun 13, 2022 at 22:59
  • @CertainPerformance Done, but it's being labelled as duplicated. Commented Jun 15, 2022 at 14:03

1 Answer 1

0

I'm not sure I understand the point of what you want to do, but your function simply returns true if your condition is false because your code continues to execute outside your if block, i.e. outside your condition since it is false.

Why not do this to return the result of your condition directly ?:

function check_condition() {
    return first_condition;
}
Sign up to request clarification or add additional context in comments.

2 Comments

"...but your function simply returns true if your condition is false..." are you sure? What am I missing?
@bloodyKnuckles I think so, why would it be the opposite if it describes exactly that? The pseudo code is as follows: If condition validated, then return false, otherwise continue after the if block, return true.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.