- 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.
- Google Apps Script for Beginners
- 微服務(wù)與事件驅(qū)動(dòng)架構(gòu)
- Oracle數(shù)據(jù)庫(kù)從入門到運(yùn)維實(shí)戰(zhàn)
- PostgreSQL 11從入門到精通(視頻教學(xué)版)
- C語(yǔ)言課程設(shè)計(jì)
- 精通MATLAB(第3版)
- LabVIEW虛擬儀器入門與測(cè)控應(yīng)用100例
- 細(xì)說(shuō)Python編程:從入門到科學(xué)計(jì)算
- Raspberry Pi Robotic Blueprints
- Python全棧開發(fā):基礎(chǔ)入門
- H5+移動(dòng)營(yíng)銷設(shè)計(jì)寶典
- Mastering Apache Camel
- React and React Native
- 程序員面試金典(第6版)
- Serverless工程實(shí)踐:從入門到進(jìn)階