TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Question: why this snippet of code won't work ?

Forums > 100 Days of SwiftUI

@boat  

I ran in Playground. it doesn't work


    var teamA = 0
    var teamB = 0
    var teamAwin = false

    func teamAResult(teamA:Int, teamB:Int) -> Bool {
        if teamA > teamB {
        return teamAwin == true
    }
    }
teamAResult(teamA:7, teamB:2)

Swift told me "error: missing return in a function expected to return 'Bool'"

But I had it return bool, right ?

Thank you in advance

Boat

2      

The function is not returning any value, try to move the return outside the if statement.

 func teamAResult(teamA:Int, teamB:Int) -> Bool {
    if teamA > teamB {
        teamAwin == true
    }
    return teamAwin
 }
 teamAResult(teamA:7, teamB:2)

3      

@boat  

why the "return" in my code doesn't count as a "return" ?

3      

You have to have a return for every path your code might take. Your original code is fine if teamA > teamB but what gets returned if teamB > teamA? You need to return something from that code path as well.

3      

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.

3      

It's perfectly fine to have the return inside the if else statement. That's not a problem at all.

The problem is as I stated: You have to have a return from every path of your code.

There are other issues with this function, but the error mentioned in the original post is due to that.

4      

@boat  

Thank you @MalbonaGenio

*"In your code the If block of code is returning something but the function is not. That's why the error is showing up." * "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."

These two sentences are very very clear explanation to me. I also very much appreciate the code you rewrite for me. Nothing better than looking at them and also try them in my playground.

@@roosterboy thanks too. I know there are lots of problem in the lines LOL. e.g I don't really need to declare everything at the beginning, right? It was because I'm quite new, I want to keep everything in written so I could remind myself later.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.