Swift – Functions


A function is a set of statements which organized together to perform a specific task. Functions are used to pass local and global parameter values inside the function calls.

  • Function Declaration – Function declaration tells about a function’s name, return type, and parameters to the compiler.
  • Function Definition – Function Definition represent the actual body of the function.

Function Definition

The function definition is represented by keyword func in swift. The function has function name with input parameters. Parameters can vary from one to many. These parameters are processed by the main body and pass the values back to the functions as output which is return type.

Function parameters are also known as ‘tuples’. A Function’s arguments should always follow the same order as the function’s parameter list and return values are followed by symbol →.


func function_name(Parameters) -> returns type {

Statement N
return parameters

See Below code in which the teacher’s name is declared as string datatype declared inside the function ‘teacher’ and when the function is called, it will return student’s name.

func teacher(name: String) -> String {
return name
}print(teacher(“First test”))
print(teacher(“Albert esal”))

we get the output as follows with play ground:: −

First test
Albert esal

Calling a Function

Let us suppose we defined a function called ‘display_number’ to Consider for example to display the numbers a function with function name ‘display_name’ is initialized first with argument ‘nos1’ which holds integer data type. Then the argument ‘nos1’ is assigned to argument ‘num’ which hereafter will point to the same data type integer. Now the argument ‘num’ is returned to the function. Here display_number () function will hold the integer value and return the integer values when each and every time the function is invoked.

func display_number(nos1: Int) -> Int {
let num = nos1
return num

print(display_number (100))
print(display_number (200))

Playground output is as follows :: −


Parameters and Return Values

Flexible function parameters and its return values from simple to complex values is present in Swift. Functions in Swift also take several forms.

Functions with Parameters

A function is called by passing its parameter values to the body of the function. Values can be passed with single or multiple parameters values as tuples inside the function.

func mult(nos1: Int, nos2: Int) -> Int {
return nos1*nos2

Running above program using playground, we get the output :: −


Functions without Parameters

Functions can also be represented without any parameters.


func func_name() -> datatype {
return datatype

Example having a function without a parameter −

func voters_name() -> String {
return “Alicey”

Running above program using playground, we get result::−


Functions with Return Values

Functions are used to return values containing string, integer, and float data type as return types. To get the largest and smallest number in a given array function ‘lasm’ is declared with large and small integer datatypes.

An array is initialized which hold integer values. We compare each and every value to get the largest and smallest value.If the value is lesser than the previous one it gets stored in ‘small’ argument, otherwise it gets stored in ‘large’ argument and the values are returned by calling the function.

func lasm (array: [Int]) -> (largest: Int, smaller: Int) {
var lar = array[0]
var sma = array[0]
for i in array[1..<array.count] {
if i < sma {
sma = i
}else if i > lar {
lar = i
return (lar, sma)
let num = lasm([40,12,-3,78,198])
print(“Largest number is: \(num.largest) and smallest number is: \(num.smaller)”)

Running Program using playground, we get result:: −

Largest number is:198 and smallest number is: -3

Functions Without Return Values

Functions have arguments declared inside the function which sometimes do not have any return values. Below program has declare a1 and b1 as arguments to  sum( ) function for addition. In this function the values of arguments a1 and b1 are passed by invoking the function call sum( ) and its values are printed there itself which eliminate the need to return values.

func sum(a1: Int, b1: Int) {
let a = a1 + b1
let b = a1 – b1
print(a, b)
sum(30, 10)

When we run the above program using playground, we get the following result −

(40, 20)
(60, 40)
(80, 68)

Dinesh Choudhary

iOS Developer with 2+ years of experience.

Dinesh Choudhary

About Dinesh Choudhary

iOS Developer with 2+ years of experience.