13. Swift: Functions, continue with advanced topic

In previous article, you learnt function declaration, parameters and return type. Lets discuss more about functions in swift.

Swift is a modern language, it provides lots of new features which other programming languages do not have. Swift helps programmers to write clean, flexible and easy to understand code.

Default function parameters

Swift provides a very nice feature where you can assign some default value to parameters. If you will not send any value to the function, the default value will be used otherwise your value will be replaced with default value. See how it works. Create a function called personInfo and pass two variables as parameters with default values as mentioned bellow.

func personInfo(name : String = "Khan", age : Int = 25){
    
    println("Person name is \(name) and age is \(age)")

}

personInfo takes two parameters, String name and Integer age, these variables are assigned some default values.

name : String = "Khan", age : Int = 25

Now call this function with no parameter as mentioned bellow.

personInfo()

Swift will not generate any error and you will see the output “Person name is Khan and age is 25“. Now call this function with one parameter

personInfo("Bob")

Now you will notice that Person name “Khan” is replaced with “Bob” and output is “Person name is Bob and age is 25″. Now call this function with 2 parameters

personInfo("Bob", 30)

Now you will notice that both values are replaced with “Bob” and 30 and output is “Person name is Bob and age is 30″

 

Parameters are constants by default.

By default parameters are constants which mean that you cannot change the value of constants. Create a function with parameter and try to assign some value to parameter

func myFunction(name : String) {

    // Swift will thow error
    name = "Bob"

    println("Person name is \(name) ")

}

Swift will immediately throw this error: cannot assign to ‘let’ value ‘name’. Which means that name is a constant and you cannot change the value of constant.

But Swift is flexible as well, it will tell you that if you are sure and confident, you can change parameter from constant to variable. You can add keyword var at the beginning of function parameter.

func myFunction(var name : String) {

    // Now its no problem
    name = "Bob"

    println("Person name is \(name) ")

}

 

External Parameter names

See this example: This function personInfo has two parameters, name and age

func personInfo(name : String, age : Int){
    
    println("Person name is \(name) and age is \(age)")

}

Pass two values to this function

personInfo(“Khan”, 30)

and it will perform its job. But is this code really self-explanatory?

When some developer will see this function call, he can guess parameters. First parameter can be Name of the person and second may be ummmmm height or waist or it can be age !!!!!!

In order to make code easier to understand, Swift provides two solutions:

1. External reference
2. External and Internal reference

1. External reference

In order to make code easy to understand, you can reference parameter name as external, when you will call this function, you can provide the parameter name in function call. To make the function parameter as an external, add # at the beginning and you are done.

func personInfo(#name : String, #age : Int){
    
    println("Person name is \(name) and age is \(age)")

}

Now when you will call this function, Swift will force you to write parameter name with value.

personInfo(name: "Khan", age: 30)

You can see that now code is more clean and self-explanatory.

2. External and internal reference

You can declare two parameter names in your function. One is for external reference and second is for internal use. Both names will be different of-course and external name will be declared first and there is no need to add # in this solution.

func personInfo(personName name : String, personAge age : Int){
    
    println("Person name is \(name) and age is \(age)")

}

When you will call this function, Swift will force you to write external “personName” and “personAge” in function call and internal “name” and “age” will be used within function body.

personInfo(personName : "Khan", personAge : 30)

 

Parameters as a list

Before closing this topic lets discuss one more point, Parameters as a list. Its very difficult to know that how many parameters a function will accept, It can be days in a week or month in a year or anything else. Swift provides very generic solution for this. Create a function and pass a variable with 3 dots …

func displayList(mylist : String...){

    for value in mylist{
        println(value)
    }   

}

Now call this function with number of parameters. All parameters will be considered as a list.

displayList("Jan","Feb","March","April")

 

 

Download file from GitHub