How To Ignore Function Results With @discardableResult

In Swift, when the function returns some value you can assign it to a variable or ignore it.

Let’s say you have your own function called makeSynchronization, it returns the Bool value-based if the synchronization was successful or not.

func makeSynchronization() -> Bool {
    
return Bool.random()
}

The most popular way to ignore function results is to use “_” instead of using an unwanted variable.

Like this:

_ = makeSynchronization()

However, you can mark your function so that it’s not required to assign a result to something.

To it, you can use @discardableResult above function declaration.

@discardableResult
func makeSynchronization() -> Bool {
    
return Bool.random()
}

Now you can execute that function and you can remove “_ = ” and the compiler won’t show any error.

makeSynchronization()

Notice that “_ = “ works as well.