Functions in Swift

A function is a set of one or multiple statements that are organized together to perform a specific task. In simple words, a function contains a block of code that can be called using the function name.

Functions are so common identifiers in every programming language. In this tutorial, we are going to learn about Swift functions and how to declare a function in Swift programming language.

A function is consists of two main parts that are given below:

  • Function declaration: It tells the compiler about the name of a function, returns type and parameters.
  • Function definition: This is the actual body of the function.

Advantage of function

The purpose of creating a function is to prevent writing the same code again and again. You write the block of code once with the function name and summon it by the name whenever we need it.

In simple words, functions help to reuse the code we write once. It will help app development easier by saving a lot of time.

Define a function in Swift

In Swift, the func keyword is used to define a function. In the time of defining a function, we can give one or several values as input parameters. We can also set the return type of the function.

Syntax of a function

Below is the syntax of a function:

func funcname(Parameters) -> returntype {
    //Some code
    return parameters
}

Calling the function

We can call a function by its name just like you can see below:

funcname(Parameters)

Example of function with code example

Below is a simple function that will add two numbers:

// Define the function
func addTwoNum() {
    let num1 = 5
    let num2 = 3
    let sum = num1 + num2
    print(sum)
}

// Caling our function
addTwoNum()

The output of the above Swift program is:

8

The function we have created above doesn’t take any parameter and also it is not returning any value. As it does not return anything, there is no need to set the return type.

Now let’s see another example where the function will take the two number as input parameters with the return type:

// Define the function
func addTwoNum(num1:Int, num2:Int) -> Int {
    let sum = num1 + num2
    return sum
}

// Calling the created function
var getSum = addTwoNum(num1: 5, num2: 3)
print(getSum)

Output:

8

In the above example, the function returns the sum instead of printing it inside the function. We are then taking it in a variable and then printing it. We can also printing it directly inside the print function instead of taking it in the variable like you can see below:

print( addTwoNum(num1: 5, num2: 3) )

The above code will return the same output: 8

Function without parameters with only return type

In the example below, you can see that we have created a function where there is no input parameters, but it has the return type:

func firstName() -> String {
    return "Thanos"
}
print( firstName() )

Output:

Thanos

 

I hope you have learned what is function and also understood how to create functions in Swift from this tutorial.

Leave a Reply

Your email address will not be published.