(Swift) Confused between a return value of a for-in loop within a function, and a return value that comes after the loop (within the function))

139 views Asked by At

I am confused with the following code.

func allTestsPassed(tests: [Bool]) -> Bool {
    for test in tests {
        if test == false {
            return false
        }
    }
    return true
}

If the code within the if-else statement (within the for-in loop) executes, it returns false.

But the code following the for-in loop is: return true.

Two different boolean values returned within a function.

When I execute print(allTestsPassed([false])) it prints 'false'.

It seems to disregard the 'return true' that follows the loop.

I do not understand.

3

There are 3 answers

0
Fahim Parkar On BEST ANSWER

return stop the process there and don't execute anything after it. It simply stop the process there.

To understand more let's update logic and see results.

func allTestsPassed(tests: [Bool]) -> Bool {
    print("start of logic")
    var currentNumber : Int = 1
    for test in tests {
        print("currentNumber=\(currentNumber) with value==\(test)")
        if test == false {
            print("returning number because data is false")
            return false
        }
        currentNumber += 1
    }
    print("end of logic")
    return true
}

Case 1 : All input is true

Now let's try case 1 where all input will be true

allTestsPassed(tests: [true, true, true, true])

Results is

start of logic
currentNumber=1 with value==true
currentNumber=2 with value==true
currentNumber=3 with value==true
currentNumber=4 with value==true
end of logic

Case 2 : One of the input is false

allTestsPassed(tests: [true, true, false, true])

Result is

start of logic
currentNumber=1 with value==true
currentNumber=2 with value==true
currentNumber=3 with value==false
returning number because data is false

If you see in second case you don't see end of logic because it do the return when test=false

Hope the logs will clears your understanding...

0
tomerpacific On

Let's explain the function you posted in detail:

func allTestsPassed(tests: [Bool]) -> Bool {
    for test in tests {
        if test == false {
            return false
        }
    }
    return true
}

This function has a for loop that iterates over a method that receives an argument that is an array of booleans and returns a boolean.

The condition within the for loop checks each element in the array and sees if it's value is false. If it is, the method allTestsPassed returns false since (I guess) one of the tests failed.

If none of the arguments in the array evaluate to false, then it means (again, guessing), that all the tests passed and that is why the for loop finishes its iteration and gets to the return true line.

In general, when you put a return statement in code, that means that any other line below it will not execute. Since in your method, the first return statement is surrounded by a condition, it will only execute if the condition evaluates to true.

Hope that clears things up a bit.

0
Sweeper On

return does not just specify what value the function should return - it also returns the control to the caller. When a return statement in allTestsPassed is executed, no other code in allTestsPassed will be executed. Execution continues at wherever you called allTestsPassed.

It is similar to a break or continue, in the sense that it causes execution to "jump" to somewhere else.

See the Swift Language Reference:

A return statement occurs in the body of a function or method definition and causes program execution to return to the calling function or method. Program execution continues at the point immediately following the function or method call.

If you were calling it like this:

print(allTestsPassed([false]))

Passing [false] would cause the return in the if statement to execute, so the rest of allTestsPassed are not executed. Execution returns to the caller. In this case, print is called with whatever value is returned.