Swift - Function Function Return

Introduction

Functions are not required to return a value.

To make a function to return a value, use the -> operator after the function declaration.

The following function returns an integer value:

func myFunction(num1: Int, num2: Int, num3: Int) -> Int {
     return num1 + num2 + num3
}

You use the return keyword to return a value from a function and then exit it.

When the function returns a value, you can assign it to a variable or constant:

Demo

func myFunction(num1: Int, num2: Int, num3: Int) -> Int {
     return num1 + num2 + num3
}

//value returned from the function is assigned to a variable
var sum = myFunction (5,6,7)

Return values from a function can also be ignored:

//return value from a function is ignored
myFunction(5,6,7)

Related Topics