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

Introduction

Functions are a core tool in a programmer's toolkit for writing maintainable code. The concept of a function is common in almost every programming language. Functions have different names in various languages: procedures, routines, and many more, but they all have two main characteristics in common:

  • They represent a sequence of instructions grouped together.
  • The sequence of instructions is identified by a name, which can be used to refer to the function.

The programmer can call, or invoke a function when the functionalities provided by the function are needed.

When the function is called, the sequence of instructions is executed. The caller can also provide some data to the function to be used in operations within the program. The following are the main advantages of using functions:

  • Reduces repetition: It often occurs that a program needs to repeat the same operations in different parts of the codebase. Functions allow us to write a single implementation that is carefully tested, documented, and of high quality. This code can be called from different places in the codebase, which enables code reusability. This, in turn, increases the productivity of the programmer and the quality of the software.
  • Boosts code readability and modification: Often, we need several operations to implement a functionality in our program. In these cases, grouping the operations together in a function and giving a descriptive name to the function helps to express what we want to do instead of how we do it.

    Using functions greatly increases the readability of our code because it's now composed of descriptive names of what we are trying to achieve, without the noise of how the result is achieved.

    In fact, it is easier to test and debug as you may only need to modify a function without having to revisit the structure of the program.

  • Higher level of abstraction: We can give a meaningful name to the function to represent what it should achieve. This way, the calling code can be concerned with what the function is supposed to do, and it does not need to know how the operations are performed.

    Note

    Abstraction is the process of extracting all relevant properties from a class and exposing them, while hiding details that are not important for a specific usage.

    Let's take a tree as an example. If we were to use it in the context of an orchard, we could abstract the tree to be a "machine" that takes a determined amount of space and, given sunlight, water, and fertilizers, produces a certain number of fruits per year. The property we are interested in is the tree's fruit production ability, so we want to expose it and hide all the other details that are not relevant to our case.

    In computer science, we want to apply the same concept: capture the key fundamental properties of a class without showing the algorithm that implements it.

A prime example of this is the sort function, which is present in many languages. We know what the function expects and what it is going to do, but rarely are we aware of the algorithm that is used to do it, and it might also change between different implementations of the language.

In the following sections, we will demystify how function declaration and definition works.

主站蜘蛛池模板: 阜新市| 前郭尔| 永胜县| 禄劝| 廉江市| 西峡县| 明溪县| 英德市| 漠河县| 阿克陶县| 竹山县| 满城县| 边坝县| 遂川县| 凤冈县| 镇江市| 五华县| 梨树县| 曲水县| 宜丰县| 九江县| 康定县| 威宁| 福鼎市| 凤冈县| 渝北区| 灵武市| 平顺县| 巴林左旗| 五寨县| 吉木萨尔县| 永清县| 都兰县| 安岳县| 丰镇市| 商都县| 玛沁县| 萨迦县| 高密市| 河津市| 海口市|