As the error that you saw: "error: missing return in a function expected to return 'Bool'"
This means that Swift is expecting the function to return some value or result, in your case a Bool.
In your code the If block of code is returning something but the function is not. That's why the error is showing up.
The if part is used to assign the true value in case teamA > teamB, because you defined a value as false var teamAwin = false
you don't need to write the else statement.
func teamAResult(teamA:Int, teamB:Int) -> Bool {
if teamA > teamB {
teamAwin = true
} else {
teamAwin = false
}
}
teamAResult(teamA:7, teamB:2)
This will show you the same error because even that you cover both cases, the function still expects you return a value. So you can write it more simple like:
func teamAResult(teamA:Int, teamB:Int) -> Bool {
if teamA > teamB {
teamAwin = true
}
return teamAwin
}
teamAResult(teamA:7, teamB:2)
This will return the final value of teamAwin, either the false value that you defined in your variable or the new value (true) if teamA > teamB.
It's not that your return does not count as return but that swift is expecting a return at a specific place, in this case at the end of the function, not the if/else statement.
If you write the function in a way that does not expect you to return a Bool your original code will work fine.
Try erasing the -> Bool
from your fucntion an see what happens.
func teamAResult(teamA:Int, teamB:Int) {
if teamA > teamB {
return teamAwin = true
}
}
teamAResult(teamA:7, teamB:2)
This is a way to long reply but I hope it helped you understand it a bit better.