Swift Programming Language: Functions and Closures

Hello Guys.

Functions, Classes and Closures are reference type in Swift.

Functions:

Function is a basic concept in programming. Now we start from syntax in swift.

Functions Syntax

Here func is a keyword. After that function name and then params and after that return value. [ Simple ].

One very important thing “In swift every parameter by default is constant, and if you want to change the value of the param then you need to tell them explicitly. Below image will clear this thing.

Functions Params

If we have a void return type, then we have two options, to write them, as shown below.

Void return type function

Multiple parameter functions:

Multiple arguments

For function parameters, we have one more concept. In swift we have External and Internal name of parameters.

In above image, you can easily see both external and internal params name. Basically internal name mean, which we use in body of a function and external name mean, when some dev call a function, he/she will give the external name. In this way code is more readable. And if we want to use same name for external and internal we can use # sign as shown below.

Same external and internal names

 

Default value functions:

We have default value functions in Swift.

Default Functions

Default Value Function Call

Now there is small issue which i shown in below image.

Default Value Function Call with Error

When we call a default value function we will face this error. To resolve this issue we need to use external parameter name as shown below.

Default Value Function Call without Error

 

Multiple Default Value Parameters:

In this section. I am not explaining any thing. So focus on the below image and learn from that, on your own. One more thing, here I use a function name with special character. Because as we know in swift we can use special character for names.

Multiple Default Value Params

Variadic Parameters:

When we want to send zero or more value in a one parameter, at that time we use this operator …” . In Java we use ellipse or varargs for this type of params.

Ellipse Params

InOut Parameters or Pass By Reference:

If we want to use parameters as a reference, at that time we use inout keyword.

InOut Params

So I think, every thing is explained in above image. Only I tell you one thing. When you call inout param function, there you send a parameter with ‘&’ sign as shown in above image.

Function Types:

“Every function has a specific function type, made up of the parameter types and the return type of the function.”

Simple as we have Int type same we can have function type. And we can use that, to achieve static polymorphism behaviour in our programs.

Function types

We have three functions, having same params and return types. So we can say, we have a Function type (Int,Int) -> Int. Now i declare a ‘binaryOperator’ function type variable. After that I use that variable and assign different functions on different locations. You can easily see the different results in the above image.

Closures:

Basically closures are the block of code. Which we use in our programs. Now some people ask question so what is the difference between function and closures. Because function is also a block of code. Yes, Function is a special type of Closure.

Before starting closure I also tell you one thing, in some languages we use Anonymous functions they are same like closure or in Objective C block also same like closures.

Now in first glance closure may look like very ambiguous. So i am staring in different manner.

Closures

In above image first define block is a function.

Second block is a Simplest closure but we get a error. But no problem we only want to know what is closures simplest form.

Third is a closure which we assign to a constant. Basically closure has no name.

Closure

Now in above image we send a helloWorldClosure to a function useClosure which accept a closure and call the code of that closure. This is also a very basic example which we did not use in our programs but i am moving very slowly. So in the end we will know what is closure :). Now here i use ( )->( ) this syntax. This syntax I will explain in next paragraph. So do not take tension.

Closure

In above image we make a function which accept a closure which having no params and no return type. After that we send a closure to that function.

The same thing we can do in this manner as shown below.

Closure

 

I only show you some simple and little complex Closures.  And from now, I am starting the discussion about how to define closures.

 

Step 1: Now first we define a block using brackets

{

}

Step 2: Now i want to send one String param in Closure so i will define in this manner as shown below.

{ ( param1: String)

}

Step 3: I also want to return Int

{(param1: String) -> (Int)

println(param1)

return 5;

}

Step 4:

{(param1: String) -> (Int) in

println(param1)

return 5;

}

In this code we use in because compiler  use this keyword to differentiate between our body and return type .

 

Step 5: Oooops. Closure is complete. 🙂

One more real example.

Real example of Closure.

Now try to focus on above image.  Our closure section complete.

 

Facebooktwittergoogle_plusredditpinterestlinkedinmailby feather

11 thoughts on “Swift Programming Language: Functions and Closures

  1. Thanks , I’ve just been searching for info about this topic for a
    long time and yours is the greatest I have came upon so far.
    However, what in regards to the bottom line? Are you positive
    about the source?

  2. Just desire to say your article is as astounding. The clearness in your publish is simply cool and that i could suppose you’re knowledgeable in this subject.
    Fine together with your permission allow me to grasp your RSS feed to stay updated with coming near near post.

    Thanks one million and please keep up the rewarding work.

  3. Admiring the dedication you put into your website and detailed information you provide.
    It’s awesome to come across a blog every once in a while that isn’t the same outdated rehashed material.
    Fantastic read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  4. Magnificent web site. Lots of helpful information here.
    I’m sending it to a few friends ans also sharing in delicious.
    And certainly, thank you for your sweat!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.