Function and Closure in Swift


A function is a collection of statements that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.

In Swift :

Use func keyword to define a function.

Call a function by following its name with a list of arguments in parentheses.

Use -> to separate the function parameters and its return type.

func fullName(firstName:String,lastName:String)-> String {

    return "Hello (firstName) (lastName)"


fullName("Milan", lastName: "Sanathara")

// Output : Hello Milan Sanathara

Define Function

# Type Description
1 Keyword To define function use “func” keyword.
2 Function Name Every function has a function name, which describes the task that the function performs.
3 Arguments Function input values that use in the function body.
4 Arrow Indicate that function has a return type.
5 Return Type Return values type.
6 Function Body Set of statements that perform a task.
7 & 8 Call Function Call a function with arguments.
func function_name (Parameter 1, Parameter 2, Parameter N) -> returntype {

   Statement 1
   Statement 2
   Statement 3
   Statement N

   return data

Function Return Type

In swift, you can return simple value like Int OR return collection like [Int].

Return Simple Value
func addition (x:Int,y:Int) -> Int {
    var sum = 0   
    return x+y

print(addition(10, 25))

// output : 35
Return Collection Value
func fullname (first:String,last:String) ->[String] {
    return [first,last]

print(fullname("Hello", last: "World"))

// output : ["Hello","World"]

Function In Function

Functions can be nested that means you can define a new function in your current function body. Nested can access variables that were declared in the outer function.

You can use nested functions to organise the code in a function that is long or complex.

func addFive() -> Int {

    var y = 5
    func sum() {
        y += 5
    return y


// output : 10


A closure is blocks of code that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as closing over those constants and variables, hence the name “closures”. Swift handles all of the memory management of capturing for you.

Global and nested functions, as introduced above Functions, are actually special cases of closures.Closures take one of three forms:

  • Global functions are closures that have a name and do not capture any values.
  • Nested functions are closures that have a name and can capture values from their enclosing function.
  • Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.

Closure Expression Syntax

{ (parameters) -> return type in



Closure expression syntax can use constant parameters, variable parameters, and input parameters. Default values cannot be provided. Variadic parameters can be used if you name the variadic parameter. Tuples can also be used as parameter types and return types.

Functions are actually a special case of closures. You can write a closure without a name by surrounding code with braces ({}). Use in to separate the arguments and return type from the body.

var numbersCollection = [20, 19, 7, 12]

//    (number: Int) -> Int in
//    let result = 3 * number
//    return result
// })

let mappedNumbers ={ number in 3 * number })

// output [60, 57, 21, 36]

I hope you will find this post very useful regarding Functions and closures in Swift. Let me know if you have any question regarding your Start-up Idea or Project. I will reply to you ASAP.

Have you got a cool idea about iPhone App Development or Web application Development ? Contact us Now to get a free consultation on your idea. Alphanso Tech is rated as one of the best iPhone App Development Company in India.

Post by Milan Sanathara

I am iOS Application Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

Leave a Reply

Your email address will not be published. Required fields are marked *