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

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.

主站蜘蛛池模板: 刚察县| 潮州市| 绩溪县| 嘉祥县| 龙门县| 龙泉市| 淮安市| 邢台县| 博乐市| 冀州市| 东至县| 和田市| 镇雄县| 霍林郭勒市| 浦城县| 绥江县| 志丹县| 兴文县| 亳州市| 宜兰市| 神农架林区| 图们市| 汕头市| 玉林市| 军事| 阳东县| 高清| 广饶县| 西贡区| 盐山县| 漳平市| 旬邑县| 无极县| 淮南市| 阳春市| 郓城县| 罗定市| 齐河县| 神农架林区| 雷州市| 洪泽县|