Function and Closure in Swift

Function

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.

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.

Function Return Type

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

Return Simple Value

Return Collection Value

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.

Closures

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

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.

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 *