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. However, it brings 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, and we would use structs and function pointers. FP relies mostly on functions, just as procedural languages relied on procedures. We can 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. This 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 cannot because of language and SDK limitations, but attempting it will introduce us to the capabilities of FP. This is why we will have a whole chapter on functions before talking about other building blocks such as types, 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 functions 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 other building blocks; 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 FP paradigms, we need to understand the capability of functions in detail. We need to consider whether we can avoid classes and only use functions. We will cover all the details related to functions within this chapter.