Functions are a built-in form of encapsulation supported by Python. By breaking down lengthy blocks of code into functions and leveraging layered function calls, we can decompose complex tasks into simpler ones. This decomposition approach is known as procedural programming, where functions serve as the fundamental building blocks.
Functional Programming can be categorized under procedural programming in a broad sense, but its core philosophy is much closer to mathematical computation.
First, we need to clarify the concepts of Computer and Compute.
At the hardware level of a computer, the CPU executes instruction codes for arithmetic operations (addition, subtraction, multiplication, division), as well as various conditional judgments and jump instructions. Therefore, assembly language is the programming language that most closely aligns with the computer’s underlying architecture.
Computation, on the other hand, refers to calculation in the mathematical sense. The more abstract the computation is, the farther it gets from computer hardware.
Translating this to programming languages: low-level languages are closer to the computer hardware, feature low abstraction, and offer high execution efficiency (e.g., the C language). In contrast, high-level languages are closer to mathematical computation, feature high abstraction, but have relatively lower execution efficiency (e.g., the Lisp language).
Functional programming is a programming paradigm with a very high level of abstraction. In purely functional programming languages, functions contain no variables. Thus, for any given function, as long as the input is fixed, the output is guaranteed to be fixed. Such functions are called side-effect-free functions. In programming languages that allow variables, however, the state of variables inside a function can be unpredictable. The same input may yield different outputs, making such functions side-effect-prone.
A key characteristic of functional programming is that it allows passing functions as arguments to other functions, and also allows returning a function as a result.
Python provides partial support for functional programming. Since Python permits the use of variables, it is not a purely functional programming language.