Functions in Swift with examples


A function is a set of reusable code blocks or statements with an organized way to perform some specific task.

In this tutorial, you are going to learn about functions in Swift programming with examples.

The syntax of the unified function in Swift is flexible enough that can easily express anything either it is simple C-style without parameter name to Objective-C style method with parameters. Even each parameter can have names and argument labels.

How to declare a function in Swift?

In Swift, you can define a function using the “func” keyword. During defining a function, you can also give it one or multiple input parameters.

Below is the syntax for creating a function in Swift programming:

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

In the above syntax, the Parameters and returntype are optional. That means you can create a function without any parameter and return type.

After creating the function, we have to call it. The statements inside the function will not run until we call the function in our program.

We have to call the function with the function name.

Now. let’s create our first function:

func cityName() {
    print("Chicago")
}

If we run the above code, it will not show anything in output even though we are printing something within the code. The code inside the function will run only when we call it.

To use the function, we have to call the function by the function name. In our case, the function name is cityName. So below is the code to call our function:

cityName()

After we call it, it will show the output:

Chicago

The above example of the function is the most simple one. it doesn’t have any input parameters and argument labels. Also, it doesn’t have any return type.

Why use functions?

We all are aware of the fact that time is a valuable thing. So it is important to save time as much as possible. A function can help us to save a lot of time.

By creating functions you can prevent yourself from writing the same block of code again and again.

Another thing is that defining and using those functions will help you to write less code that makes sense. It makes the source code clean and easily understandable.

You can distribute some of the specific tasks within functions and then call the function when you need to perform the specific task without writing the whole code again.

Creating a function with parameters

Now see another example below:

func country(countryName: String) {
   print(countryName)
}

country(countryName: "Japan")
country(countryName: "Canada")

Output:

Japan
Canada

In this example, we have a string type input parameter. We are printing the input.

Function with return type ( Return value from function)

We can also return a value from a function. The examples above don’t have any return type. Also, not returning anything.

But in the real-world app-development project, we have to return a value instead of printing it in the terminal.

The type of returned information may be like any other type in Swift. It can be Integer, String, Float and so on…

So below is the example of a Swift function that returns a string type value:

func country() -> String {
   return "United Kingdom"
}

print( country() )

Output:

United Kingdom

In the above example, we are returning a string type value in the function.

Now see another example of a function where we will set a parameter with return type:

func country(countryName: String) -> String {
   return "The country is "+countryName
}

print( country(countryName: "Germany") )

Output:

The country is Germany

In the above example, we have concatenated the parameter with the string “The country is “.

Calling a function

To call the function we have to use the name of the function. It is the name, we gave it during defining it.

In the above code examples, we call each of the functions to make it work. Now we will see it separately.

Let’s see the example step by step:

Creating our function:

func temperature(tempValue: Int) -> Int {
   return tempValue
}

And then calling it:

temperature(tempValue: 21)

After run the code, we will see the output given below:

21

Function with multiple parameters

Another interesting fact about functions in Swift is that you can set multiple parameters for one function. You have to separate each parameter with a comma just like you can see in the given syntax below:

func functionName(parameter1: Type, parameter2 Type, parameter3 Type...., parameterN: Type) -> returntype {
   // statements in the function
}

For example, below we have created a function to calculate the summation of two numbers:

func sumNumbers(num1: Int, num2: Int) {
    let sum = num1 + num2
    print(sum)
}

sumNumbers(num1: 5, num2: 3)
sumNumbers(num1: 5, num2: 13)

Output:

8
18

Function With Argument Labels

During defining a function in Swift, we can also set argument labels. It will make the function expressive and sentence-like which is easy to understand.

Below is the example of a function with argument labels:

func sum(of num1: Int, and num2: Int) {
  let sum = num1 + num2
  print(sum)
}

sum(of: 12, and: 8)

Output:

20

As you can see, we are able to make the function calling more understandable and just like a meaningful English sentence – sum(of: 12, and: 8) . It makes more sense calling a function like this.