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

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.

主站蜘蛛池模板: 河津市| 乌兰察布市| 改则县| 祁连县| 龙门县| 饶平县| 汶川县| 邵阳市| 扎兰屯市| 永济市| 慈利县| 青冈县| 环江| 堆龙德庆县| 铜川市| 通山县| 临漳县| 三河市| 锡林浩特市| 融水| 阿拉善左旗| 泸水县| 东乡族自治县| 怀化市| 木里| 洮南市| 大丰市| 始兴县| 和政县| 迁西县| 维西| 富顺县| 柳河县| 崇义县| 贵德县| 青神县| 萨嘎县| 巴彦淖尔市| 攀枝花市| 巫溪县| 天长市|