Ticker

6/recent/ticker-posts

Functions in Swift

Functions

Functions are self-contained chunks of code that performs a some special task, and the functions will be call when  needed. Functions in Swift are more over similar to C functions and the Objective-C methods.

Syntax of function

To declare any function you need to use func keyword, if see at function syntax it has four segments say name, parameters, return type and function body.

name: It is the name of particular function.
parameters: Are the arguments or members to be passed to the function.
return type: The function may or may not returns something to the caller. If the caller is expecting some return value from the function then the return type will be a object or primitive data type, if the caller is not expecting return value from the function then the return type will be Void type.
function body: Is a block, where you can write your code to achieve something with it.

Defining and Calling Function

After understanding syntax of function, it very easy to define and calling a function.
func welcome(language: String) -> String {
    return "Welcome to \(language)"
}

print(welcome("Swift"))
Incase if you don't expecting any return value from  function
func doNothingWithMe() -> Void {
    //function body
}

doNothingWithMe()
In the function parameters section of Swift we have named parameters and simple parameters, as you see in the welcome function you are just passing language as parameter. If you want to understand about what is named parameter let's see the below.
func welcome(toTheLanguage language:String) -> String {
    return "Welcome to \(language)"
}


print(welcome(toTheLanguage: "Objective-C"))
In the above example we just modified language parameter to named parameter toTheLanguage, that means the named parameter is user understandable.

Functions with Multiple Parameters

The function may required multiple parameters as it's input. When you defining functions with multiple parameters, the named parameters helps us to understand what exactly the function could do with those multiple parameter.
func fullName(ofUserFirstName firstName:String, andLastName lastName:String) -> String {
    return "\(firstName\(lastName)";
}

fullName(ofUserFirstName: "Jayachandra", andLastName: "Agraharam")

Functions with Multiple Return values

The Swift has a ability to return multiple values from the same function.
func minMax(array: [Int]) -> (min: Int, max: Int) {
    var currentMin = array[0]
    var currentMax = array[0]
    for value in array[1..<array.count] {
        if value < currentMin {
            currentMin = value
        } else if value > currentMax {
            currentMax = value
        }
    }
    return (currentMin, currentMax)
}


let bounds = minMax([12,32,43,65,2,0890,34,5,0])
print("min : \(bounds.min) and max : \(bounds.max)")

Conclusion 

The swift programming language has huge changes in function defining and calling compared to Objective-C. To know more about functions go here.

Post a Comment

0 Comments