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

  • C++ Fundamentals
  • Antonio Mallia Francesco Zoffoli
  • 548字
  • 2021-06-11 13:35:59

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.

主站蜘蛛池模板: 阿拉尔市| 阳原县| 萍乡市| 南漳县| 本溪| 旌德县| 仪陇县| 丰县| 桂东县| 镇原县| 雷州市| 明水县| 荔浦县| 轮台县| 永安市| 宁南县| 额尔古纳市| 卫辉市| 湘潭市| 忻城县| 广昌县| 虹口区| 高雄市| 九龙县| 博湖县| 宣恩县| 霍山县| 洪泽县| 马公市| 博爱县| 大兴区| 景泰县| 浦县| 偏关县| 云南省| 忻州市| 黄冈市| 石柱| 平遥县| 麟游县| 易门县|