Better Programming

Advice for programmers.

Member-only story

A Deep Dive Into Functions in Swift

Neel Bakshi
Better Programming
Published in
6 min readMar 9, 2021

--

Photo by Maxwell Nelson on Unsplash

By and large, functions are a basic component of any programming language and is one of the first things that you learn when you start to code as well. In this article we’ll go through the basics and then also delve into some of their details.

Basics: Functions and Closures

Functions and Closures are both constructs provided by Swift to perform a set of tasks within itself. They take in 0 or more parameters which might be needed to performs the tasks and they can optionally return a value.

Functions

Let’s start with functions, since they’re easier to understand.

There are five parts to a function:

  1. The keyword func. This keyword signifies the start of a function definition.
  2. The name of the function.
  3. The parameters (named or un-named)
  4. The body of the function.
  5. The return type followed by a -> eg: -> String

--

--

Neel Bakshi
Neel Bakshi

Written by Neel Bakshi

Guy who handles everything mobile @headout among other things! Ex @practo