Swift - functions
Tags: swift, programming, beginners, tutorial
Published: 2021-12-28 16:38:54

Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.

## Defining and calling function

```swift
func greet(person:String) -> String {
return "Hello, " + name
}

greet(person:"Ajith")
//Hello, Ajith
```
### points to note
- Functions can be without parameters (returns same value)
- functions can be with multiple parameters
- function without Return values (the function definition doesn't include the return arrow (->) or a return type.
- The return value of a function can be ignored when it is called.
- function can return multiple return values (tuples)

## Function argument labels and parameter names
Each function parameter have the both an argument label and parameter name.
**The argument name is used when calling the function**
Each argument is written in the function all with its argument label before it.
**The parameter name is used in the implementation of the function.**

**note: By default parameters use their parameter name as their argument label.**

```swift
func functionName(argument label parameter Name:Int){
//function body
}

functionName(argument label: arg)
```
## variadic parameters
A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (...) after the parameter’s type name.

```swift
func arithmeticMean(numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers
```

**Note:** Since the function can have multiple parameters the first parameter that comes after a variadic parameter must have a argument label.

## In-out parameters
Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.

```swift
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"

```

**Note:** An In-out parameters can't have default values, and variadic parameters can't be marked as in-out.

## Function type as parameter type
we can use a function as a parameter to another function.

```swift
func addTwoInts(_ a: Int, _ b: Int) -> Int {
    return a + b
}

func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 5)
// Prints "Result: 8"

```

## function type as return type
You can use a function type as the return type of another function. You do this by writing a complete function type immediately after the return arrow (->) of the returning function.

## Nested function
So far in this blog have been examples of global functions, which are defined at a global scope. You can also define functions inside the bodies of other functions, known as nested functions.