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

  • 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.

主站蜘蛛池模板: 印江| 神木县| 格尔木市| 博野县| 潼南县| 顺义区| 昌邑市| 昌宁县| 娄烦县| 赣州市| 雅安市| 甘洛县| 夏河县| 方山县| 鄂伦春自治旗| 称多县| 长兴县| 始兴县| 福泉市| 个旧市| 罗定市| 平顶山市| 江陵县| 扬中市| 和龙市| 苏尼特左旗| 邻水| 平邑县| 土默特左旗| 盐山县| 金塔县| 桦甸市| 抚远县| 滦平县| 巴东县| 康平县| 玛纳斯县| 云南省| 冀州市| 湖北省| 涟源市|