Hello Guys.
Functions, Classes and Closures are reference type in Swift.
Function is a basic concept in programming. Now we start from syntax in swift.
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.
If we have a void return type, then we have two options, to write them, as shown below.
Multiple parameter functions:
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.
Default value functions:
We have default value functions in Swift.
Now there is small issue which i shown in below image.
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.
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.
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.
InOut Parameters or Pass By Reference:
If we want to use parameters as a reference, at that time we use inout keyword.
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.
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.
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.
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.
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.
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.
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.
Now try to focus on above image. Our closure section complete.
by
Excellent site you have got here.. It’s difficult to find
good quality writing like yours these days. I seriously appreciate individuals like you!
Take care!!
Hello my friend! I want to say that this article
is awesome, great written and include almost all vital infos.
I’d like to look extra posts like this .
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?
Hello to every one, the contents present at this web site are actually
awesome for people experience, well, keep up the nice work fellows.
I am really glad to glance at this blog posts which contains tons of useful data, thanks for providing such statistics.
Excellent post! We will be linking to this particularly great content on our
website. Keep up the great writing.
What a information of un-ambiguity and preserveness of precious experience
about unpredicted feelings.
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.
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.
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!