iOS Fundamentals — iOS Tour Part 4(Function and Closures)

Dipika Kansara
4 min readFeb 3, 2022

In this tutorial, you will learn about the Swift function, function expressions and basic fundamentals of closures with the help of examples.

Swift Function :

A function is a block of code that performs a specific task. We can use func to declare a function.

In Swift, there are two types of function:

  • User-defined Function — We can create our own functions based on our requirements.
  • Standard Library Functions — These are built-in functions in Swift that are available to use.

Function Declaration:

Here, Call a function by following its name with a list of arguments in parentheses. Use -> to separate the parameter names and types from the function’s return type.

func functionName(parameters)-> returnType {
// function body
}

Here,

  • func — keyword used to declare a function
  • functionName — any name given to the function
  • parameters — any value passed to function
  • returnType — specifies the type of value returned by the function

let’s see an example for declare a simple function.

func introduction() {
print("Hi This is Dipika and I am an iOS developer")
}

Function Calling:

In the above example, we have declared a function named introduction(). Now, to use this function, we need to call it. Here’s how we can call the introduction() function in Swift. When the function is called, the program goes to the function definition and all codes inside the function will be executed. The control of the program jumps to the next statement after the function calling.

let’s see an example for calling a function.


func introduction() {
print("Hi This is Dipika and I am an iOS developer")
}
// calling a function
introduction()

Function with Parameters:

A parameter is a value that is accepted by a function. Also this type of example always asking by interviewers.

let’s see an example for declare a function with parameters.

// function with two parametersfunc addtwoNumbers(no1: Int, no2: Int) {  var result = no1 + no2
print("Sum: ",result)
}
// function call with two values
addNumbers(no1: 1, no2: 7)

Function with parameters and return types :

A Swift function may or may not return a value. If we want our function to return some value, we can use the return statement by using -> . For example,

func addtwoNumbers(no1: Int, no2: Int) -> Int {
var result = no1 + no2
return sum
}

// calling function with two values
var output = addtwoNumbers(no1: 1, no2: 4)

print("Sum: ", output)

Swift Closures:

Functions are actually a special case of closures: blocks of code that can be called later. The code in a closure has access to things like variables and functions that were available in the scope where the closure was created, even if the closure is in a different scope when it’s executed

We don’t use the func keyword to create closure. Here's the syntax to declare a closure:

{ (parameters) -> returnType in
// statements
}

Here,

  1. parameters — any value passed to closure
  2. returnType — specifies the type of value returned by the closure
  3. in (optional) — used to separate parameters/returnType from closure body

Here is an example for declare and calling the closures,

var introduction = {
print("Hi This is Dipika and I am an iOS developer")
}
introduction()

Closures with Parameters:

In this example, we have assigned a closure to the user variable.

Inside the closure, (name: String) specifies that the closure accepts the String type parameter named. Notice that we have used in to separate closure parameter with body.

let user = { (name: String)  in
print("Hey there, I am \(name) and I am an iOS Developer")
}

// closure call
user("Dipika")

Closures with Parameters:

A Swift closure may or may not return a value. If we want our closure to return some value, we need to mention it’s return type and use the return statement. For example,

// closure definition
var findSquare = { (num: Int) -> (Int) in
var square = num * num
return square
}
// closure call
var result = findSquare(3)
print("Square:",result)

Benefits of Using Functions and Closures :

1. Code Reusable — We can use the same function and closures multiple times in our program which makes our code reusable.

2. Code Readability — Functions help us break our code into chunks to make our program readable and easy to understand.

I have covered declare and calling the swift function and closures. also I have covered here calling function and closures with parameter and return types and what are the benefits of declaring functions and closures here.

I hope I have explained basic and important topics in swift in simple way. and it might be helpful in learning basics and basic preparation for an interview.

And finally, Happy Coding 😁

--

--