DEV Community

Agoi Abel Adeyemi
Agoi Abel Adeyemi

Posted on

Functions in Swift

Introduction

Functions are self-contained chunks of code that perform specific tasks.

Defining and Calling a Function

Right below is a simple function, that actually does nothing at the moment.

func emptyFunction() { //your code should go inside here}
Enter fullscreen mode Exit fullscreen mode

The func keyword is use to define a function while the emptyFunction is the name given to the function. You must use the func anytime you create a function, and the function name can vary base on what the function will do.

The above function can be called within my code like below

emptyFunction()
Enter fullscreen mode Exit fullscreen mode

Function Parameters

There are times when we need to pass parameters
to a function, we can pass parameters to a function like below

func functionWithParameter(parameterOne: Int) { 
    print("This is my parameter: \(parameter)") 
}
Enter fullscreen mode Exit fullscreen mode

The paramterOne is the parameter passed into the function while the Int denote the data type of the parameter. The data type (Int)serves as a kind of contract that must be adhere to when we call the function meaning, the parameter must be a type of Int.We need to include the parameter we passed when calling the function like below

functionWithParameter(parameterOne: 32)
Enter fullscreen mode Exit fullscreen mode

We can use more than one parameter in a function by separating the parameters with a comma

//declaring the function
functionWithParameters(parameterOne: Int, parameterTwo: String) {
}

//calling the function
functionWithParameters(parameterOne: 32, parameterTwo: "Hello")
Enter fullscreen mode Exit fullscreen mode

Function Return Types

The return type is use to denote the data type of the value our function should return. Just like the parameter type, Xcode will enforce us to make sure that the function returns the type it specified.

func functionWithReturnType() -> String { 
    return "I must return a string"
}
Enter fullscreen mode Exit fullscreen mode

There are many data types in swift which include Integer, Boolean, String, Float, Double. The -> String denotes that the function must return a type of String. Any function that does not return a value has a special return type named Void. When we do not specify a function return type, we have simply told swift that the return type is a void hence our function must not return anything.

//Different ways to declare Void

func noReturnType() { 
    //swift will automatically take this function as void 
    //hence we must not return anything
}

function returnWithReturnType() -> Void { 
    //same as above, but we declare the void ourselves
}

function returnWithReturnType() -> () { 
    //this is another way to declare void
}
Enter fullscreen mode Exit fullscreen mode

Function With More Than One Return Value & Type

There are times when we need our function to return more than one value, We can do this in swift with the help of a tuple like below

//declaring the function
myFunction () -> (value1: Int, value2: String) {
    return (value1, value2)
}

//calling the function
let result = myFunction(value1: 12, value2: "Hello from me")

//we can access the return value like below
result.value1
result.value2
Enter fullscreen mode Exit fullscreen mode

Function Parameter With Default Value

We can define a default value for any function parameter should in case we do not want to pass the value at all time

func someFunction(parameterWithDefault: Int = 32) {}
Enter fullscreen mode Exit fullscreen mode

If we call the someFunction(parameterWithDefault: 50) and pass in a parameter of 50, it will override 32 which is the default value assigned to the function, we can also call the function without a parameter.

Function Parameter External And Local Name

There are times when we need to give our function an external and local name, mostly basically for readability. The external name is use to pass the parameter into the function while the internal name is use to access the parameter inside of the function.

func myFunction(havingValue value: Int) { 
    //we can access the havingValue parameter with value 
    print(value)
}

//calling the function
myFunction(havingValue: 50)
Enter fullscreen mode Exit fullscreen mode

Notice that we use the havingValue to pass the parameter when we call the function but to access the parameter inside of the function, we use value.

Omitting Argument Labels

Each time we have called a function that has a parameter, we have been defining the parameter name like below

myFunction(label: Int) {}

//calling the function
myFunction(label: 32)
Enter fullscreen mode Exit fullscreen mode

We can omit the label when calling the function by adding a _ as the function external name like below

myFunction(_ label: Int) {}

//calling the function without the label definition
myFunction(32)
Enter fullscreen mode Exit fullscreen mode

There is more to function in swift than what I have covered here which is just an introduction to it. You can read more about swift function here on apple documentation

I am happy to share this article with you. If you’ve enjoyed this article, do show support by giving a few claps 👏 . Thanks for your time and make sure to follow me or drop your comment below 👇

Top comments (0)