官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 获嘉县| 洛阳市| 密云县| 武定县| 理塘县| 绿春县| 洛隆县| 江津市| 定安县| 揭阳市| 咸阳市| 屏山县| 兴仁县| 黄冈市| 大田县| 磐石市| 临高县| 太和县| 鄯善县| 公安县| 万全县| 长汀县| 汕尾市| 墨江| 乐平市| 浦北县| 凤阳县| 凤冈县| 秦安县| 松江区| 柞水县| 桦南县| 开江县| 龙川县| 舒城县| 绥芬河市| 石首市| 上栗县| 城固县| 特克斯县| 霍山县|