Swift functions

 

swift functions

Hi Devs, this is my first post in Onetouchcode. I am going to explain the Swift functions with a data structure called Stack. If you are new to Swift programming, check out here .

Swift Functions

A function is block of code that can be executed whenever it’s needed.

Syntax:

func function-name(parameterName: parameterDataType, ...) -> returnType {

statements
....
}

Swift functions will start with ‘func‘ keyword and followed by name of the function. And the parameters will be in closed parenthesis and return type will be  after the ‘->’ symbol. The statements will be in curly({}) braces.

Function with parameters

var stackArray = Array<Int>()

//Function with parameters
func insertIntoStack(element:Int){
    stackArray.append(element)
}

Here, insertIntoStack is a function that will add elements to stackArray.

In Swift, we can add elements to an array by using append method. And stackArray will accept only integers as we declared as Int while declaration as above.

We can call the above method as following

insertIntoStack(2)
insertIntoStack(3)
insertIntoStack(4)
insertIntoStack(5)

Now we will try to print the stackArray

print(stackArray)

output:

[2,3,4,5]

 Function with out parameters

//Funtion with out parameters
func removeFromStack(){
    stackArray.removeLast()
}

Here removeFromStack is an example of swift functions without parameters and will remove last element from stackArray.

We can call this method and print it in a console

removeFromStack()

//printing stackArray after calling removeFromStack function
print(stackArray)

output:

[2,3,4]

So, the last element of the stackArray has been removed and that element was inserted at last.

Function with return type

//Function with single return type
func getFirstElement() -> Int{
    return stackArray[0]
}

output:
2

Here, getFirstElement is a function and it will returns first element from stackArray.

Function with multiple return types

//Function with multiple return types using tuple
func getCurrentStackAndCount() -> (array:Array<Int>, count:Int){
    
    let tempStackArray = stackArray
    
    let count = stackArray.count
    
    return (tempStackArray, count)
}

print(getCurrentStackAndCount)

Output:

([2,3,4],3)

Sometimes, If we need multiple values return from a method. In Swift, we can do with Tuple. A tuple will group multiple values into a single compound value. The values will be of any type.

In our example above, getCurrentStackAndCount is a function and the return type (array:Array<Int>, count:Int) is tuple. And it contains array and count of array.

Function with variadic parameters

//Function with variadic parameters
func insertMultipleElementsIntoStack(elements:Int...){
    
    for element in elements {
        stackArray.append(element)
    }
}

A variadic parameter will accept a zero or more values of a specified type. In above example, elements:Int… is a variadic parameter. This parameter will ends with three dots.

And now we can call above method as follows:

insertMultipleElementsIntoStack(6,7,8,9,10,11)

Output:

[2, 3, 4, 6, 7, 8, 9, 10, 11]

So, we are adding multiple elements to stackArray at once.

And the following code contains all the details what we discussed till now. Just copy and the following in code in Xcode’s playground and you will get the immediate output.

var stackArray = Array<Int>()

//Function with parameters
func insertIntoStack(element:Int){
    stackArray.append(element)
}

insertIntoStack(2)
insertIntoStack(3)
insertIntoStack(4)
insertIntoStack(5)

print(stackArray)

//Funtion with out parameters
func removeFromStack(){
    stackArray.removeLast()
}
removeFromStack()

print(stackArray)

//Function with single return type
func getFirstElement() -> Int{
    return stackArray[0]
}
print(getFirstElement())

//Function with multiple return types using tuple
func getCurrentStackAndCount() -> (array:Array<Int>, count:Int){
    
    let tempStackArray = stackArray
    
    let count = stackArray.count
    
    return (tempStackArray, count)
}

print(getCurrentStackAndCount())

//Function with variadic parameters
func insertMultipleElementsIntoStack(elements:Int...){
    
    for element in elements {
        stackArray.append(element)
    }
}

insertMultipleElementsIntoStack(6,7,8,9,10,11)

print(stackArray)

 

Hope this will be helpful for few developers to understand the functions in Swift and do not forget to share it.

Thank you! & Happy Coding!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *