This is the part 1 of the series of the Tutorials of Swift programming. You can take a quick look at the Introduction to Swift.
In this Tutorials we will learn about
In this Tutorials we will learn about
Functions
Use func to declare a function. 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.
Few Points of Functions:
- Functions can return multiple values using Tuples
- Functions can accept variable number of parameters and collect them into an array.
- Nested functions
- Functions can accept other functions as parameters.
- Functions can have function as its return value. Because functions are first-class type.
- Functions are special cases of closures
func <functionName>(<parameter1Name> : <parameter1Type>, parameter2Name> : <parameter2Type>) -> <ReturnType>{
// Function Body
}
func greatDay(name: String, day: String) -> String {
return "Hey \(name) today is: \(day)"
}
And this is how you call the function. :)
greatDay("karm", "sunday")
Function returning multiple values
So What’s special about these functions from the other programming language functions? You can return multiple values from the function.
func getPrices() -> (Int, Int, Int){
return (10,20,30)
}
getPrices()
Function accepting variable parameters
Functions can take variable number of parameters and collect them into an array.
func calculateSumOf(numbers: Int...) -> Int{
var sum = 0
for number in numbers{
sum+=number
}
return sum
}
calculateSumOf()
calculateSumOf(12,23,45)
Nested Functions
Functions can be Nested. Nested function have access to the variables defined in the outer function. We can always make use of the nested function to organize the code in nested functions which is long and complex.
func returnFullName() -> String {
var firstName = "Karmjit"
func add()-> String{
var fullName = firstName.stringByAppendingString(" Singh")
return fullName
}
var name = add()
return name
}
returnFullName()
Function accepting other func as parameter
func incrementerCreator() -> (Int ->Int){ // (Int -> Int) means it will return a function which accepts int and return int
func incrementer(value : Int) ->Int{
return value + 1
}
return incrementer
}
let addOne = incrementerCreator()
addOne(8)
Function returning func
// increment parameter means it will be a function which accepts an int and return int
func incrementEveryone(list: Int[], increment:Int -> Int) -> Int[]
{
for var index = 0; index < list.count; index++
{
list[index] = increment(list[index])
}
return list
}
var items = [1,2,3,4,5]
var afterIncrement = incrementEveryone(items, addOne)
Closures
Few Points regarding Closures
- Closures can be declared using {} without specifying any name.
- Separate parameters and body using the in .
- If Closure type is already known you can omit the paraters type and return value type.
- You can access closure elements using parameter number.
Closure Syntax:
items.map({
(number: Int) -> Int in
let result = number * 3
return result
})
Single Line Closure
items.map({number in number * 3})
Accessing array items with their position
sort(items){ $0 < $1}
You can try playing with the functions and closures in Playground and learn more about them.
Until next time.....
Happy Coding....
No comments:
Post a Comment