Chapter 2. Functions and Closures

In the previous chapter, we had an overview of functional programming and the Swift programming language. It introduced some of the key concepts about functions. As functions are the fundamental building blocks in functional programming, this chapter dives deeper into it and explains all the aspects related to the definition and usage of functions in functional Swift, together with coding examples.

This chapter starts with a definition of functions, continues with other related topics such as function types and tuples, and finally concludes with more advanced topics such as first-class functions, higher-order functions, function composition, closures, currying, recursion, and memoization.

This chapter will cover the following topics with coding examples:

  • The general syntax of functions
  • Defining and using function parameters
  • Setting internal and external parameters
  • Setting default parameter values
  • Defining and using variadic functions
  • Returning values from functions
  • Defining and using nested functions
  • Function types
  • Pure functions
  • First-class functions
  • Higher-order functions
  • Function composition
  • The definition of a custom operator
  • Defining and using closures
  • Function currying
  • Recursion
  • Memoization

What is a function?

Object-oriented programming (OOP) looks very natural to most developers as it simulates a real-life situation of classes or, in other words, blueprints and their instances, but it brought a lot of complexities and problems such as instance and memory management, complex multithreading, and concurrency programming.

Before OOP became mainstream, we were used to developing in procedural languages. In the C programming language, we did not have objects and classes; we would use structs and function pointers. So now we are talking about functional programming that relies mostly on functions just as procedural languages relied on procedures. We are able to develop very powerful programs in C without classes; in fact, most operating systems are developed in C. There are other multipurpose programming languages such as Go by Google that is not object-oriented and is getting very popular because of its performance and simplicity.

So, are we going to be able to write very complex applications without classes in Swift? We might wonder why we should do this. Generally, we should not, but attempting it will introduce us to the capabilities of functional programming. This is why we will have a whole chapter about functions before talking about other building blocks such as classes, structs, and enums.

A function is a block of code that executes a specific task, can be stored, can persist data, and can be passed around. We define them in standalone Swift files as global functions or inside other building blocks such as classes, structs, enums, and protocols as methods.

They are called methods if they are defined in classes but, in terms of definition, there is no difference between a function and method in Swift.

Defining them in other building blocks enables methods to use the scope of the parent or to be able to change it. They can access the scope of their parent and they have their own scope. Any variable that is defined inside a function is not accessible outside of it. The variables defined inside them and the corresponding allocated memory go away when the function terminates.

Functions are very powerful in Swift. We can compose a program with only functions as functions can receive and return functions, capture variables that exist in the context they were declared, and can persist data inside themselves. To understand the functional programming paradigms, we need to understand the capability of functions in detail. We need to think if we can avoid classes and only use functions, so we will cover all the details related to functions in upcoming sections of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset