Introduction to Swift Function

Function is  a group of statements the are executed to perform a certain task. For example,  we want  multiplication of two numbers that user entered then for this requirement we can create a function names Multiply which takes two number as input normally called parameters and inside it use multiply operator, then return the multiply result. One question arises here that why do we need function for multiply two numbers as the same can be done by writing a bunch of lines not necessarily inside the function block. So, one of the most important reason to create function is to use same code again and again in different classes rather then writing same line of code again and again thus making our  code base heavier and not professional.
         
                                                      Swift provide us functions and syntax to create or wrote function is a mix of C and objective C. Swift function starts with func keyword. Function  has  parameters or are empty function that does not has parameters. Swift function can return values like any other normal objective C or C type function.

Declare and Define Function

func multiply (firstNumber: Int ,  secondNumber: Int) -> Int

Above line will create a function named multiply which takes two parameters firstNumber and secondNumber, both are type of int and return a int type value.

func multiply (firstNumber: Int ,  secondNumber: Int) -> Int

{
      int result = firstNumber * secondNumber
      return result
}

println(” Output of multiply functions is == ( multiply(10, 20) )”)

Output will be:

Output of multiply functions is == 200

You can also create a function that does not requires parameters to be passed and neither return a value

func emptyFunction()
{
    println(” This is an empty function. “)
}

Call the empty function as

emptyFunction()

Output will be:

This is an empty function.

Function with multiple return value

The most awesome feature of Swift functions is that they can return multiple value.

func manyOperation (firstNumber: Int , secondNumber: Int) -> (add: Int , multiply: Int , subtract: Int )
{
       add =  firstNumber + secondNumber

       multiply =  firstNumber * secondNumber

       subtract =  firstNumber – secondNumber

       return (add, multiply, subtract)
}

let result  =  manyOperation(20, 10)

println(” Result returned by manyOperation == (result.add) and (result.multiply) and (result.subtract))

Output will be:

Result returned by manyOperation ==  30 and 200 and 10

External parameter name

Swift language also provide you to pass external parameters name with local parameters name as these can be used within the body of function and cannot be used while calling the function. Main purpose of this is to make function call a more readable. Take a look at below example

func createSentence (stringFirst: String, stringSecond: String) -> String
{
     return stringFirst + stringSecond
}

println( createSentence(“Good”, “Morning”))

Now the function will give output as  Good Morning

Looking closer at the line of its getting called it is unclear that what this function is doing and thus consume more time for some one who is looking at your project after handover. Here external parameters come handy. Look at below for example of function with external parameter name

func createSentence (Combine stringFirst: String, with stringSecond: String) -> String
{
     return stringFirst + stringSecond
}

println( createSentence(Combine :”Good”, with: “Morning”))

Output will be  Good Morning.

Functions with external parameters are more like objective C functions and the one without external parameters are like C functions.