Swift Return Statement

  Total Views : 258
  Comments : 0
  Keywords : Swift return statement to return the values of function or method with examples, Swift return statement in functions to return array or dictionary or tuple with examples

Here we will learn return statement in swift with examples and how to use swift return statement to return the value based on our requirements.

Swift Return Statement

Generally in swift we use 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.

Syntax of Swift Return Statement

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.

Swift Return Statement 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 above return statement example in swift playground that will return the result like as shown below.

 

1

This is how we can use return statement in swift programming language to return values based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User