In swift, we use the return statement in functions or methods to return the values based on our requirements.
By using return keyword we can return values from functions/methods based on our requirements in swift programming language.
Following is the syntax of using return statement in swift programming language.
return
or
return expression
Now we will see how to use return statement in swift programming language with example.
Following is the simple example of using swift return statement in function to return required value based on our conditions.
func myFunc() -> Int {
let myNumber = 16 % 3
if myNumber == 0 {
return 0
}
else if myNumber == 1 {
return 1
}
return 0
}
print(myFunc())
If you observe above swift return statement example we are returning values based on our requirements.
When we run the above return statement example in a swift playground that will return the result like as shown below.
This is how we can use the return statement in a swift programming language to return values based on our requirements.