There are different types of programming. That is, they can be divided based on the features and capabilities of programming languages. In specialized terms, we talk about programming paradigms. Paradigms are categories that group programming languages with common features.
Programming languages may fall into several paradigms. It means that a language may have the possibility to be programmed in several different ways. If I want to name the important paradigms, I should mention imperative programming, procedural programming, object-oriented programming, declarative programming, and logic programming. In addition to the mentioned paradigms, the functional, functional, or functional programming paradigm is one of the main and common paradigms in programming.
In this article, I intend to introduce you to functional programming or paradigm. Because Python is one of the most important and practical languages included in this paradigm, function-oriented programming in Python has been discussed in a separate article.
Table of Contents
What is functional programming?
Functional programming is a paradigm where everything is written in a purely mathematical way. Functional programming is also a type of declarative programming. In functional (and declarative) programming the main focus is on “what to solve” while in prescriptive programming the main focus is on “how to solve”.
In the functional paradigm, everything is written based on mathematical functions. In other words, everything is expressed in conditional sentences. If in command programming a set of command sentences follow each other and by their execution the structure and state of the site is determined, in functional programming a tree structure and a branch of functions link the values to each other. Lisp, Python, Erlang, Haskell, Clojure are some of the most famous functional programming languages. Other languages such as #PHP, Katlin, Java 8 and higher, C also have function oriented programming to some extent. Accordingly, functional programming languages are divided into two categories:
- Pure Functional Languages: Languages that are only compatible with the functional paradigm, such as Haskell, and cannot be written with them to other paradigms, such as syntax.
- Impure Functional Languages: In addition to the functionalist paradigm, these languages are also compatible with the grammatical paradigm, such as Lisp.
Advantages of functional programming
- In functional programming, the programmer only has to write the result he wants, not all the steps necessary to reach that result. So the number of phrases to be written will be very less.
- The output of the function is only the value that is requested from it. Functions use immutable values. In functional programming, hidden output or additional output is not produced. This very important feature means that the number of errors is very low, and this means that debugging is very fast and easy.
- This type of programming is ideal for creating and implementing concurrency/parallelism. Because the work of each function and the value to be returned are known, the execution of one function does not prevent the execution of another function. This means that in functional programming and using functional languages, asynchronous programming is easily possible.
Disadvantages of functional programming
Along with all the advantages of this type of programming, its disadvantages should also be mentioned:
- Sometimes it may be a little difficult to write the code in pure function language;
- If we assume that writing with a purely functional language is an easy task, combining this language with other languages is not an easy task at all;
- Maybe for those who are not very familiar with math and functions, learning this type of programming is a bit difficult. Because functions, like variables and control and logical operators, are basic concepts in the Python programming language;
- Programs written in functional languages are very heavy and as a result require a lot of space to store them.
Functional programming in Python
As mentioned, Python is a functional language. Of course, Python has some capabilities and features necessary for other programming. In Python, everything is an object. Functions are objects like numbers and loops in Python. So, it can be concluded that everything that can be done in Python with numbers and loops can also be done with functions. Of course, there are some small exceptions to this conclusion. For example, in Python, two objects can be added together with the + operator. But this operator is not defined to add functions to each other.
Also, in Python, it is easy to implement the most important features of functional programming: one function can be passed to another function in the form of an argument, and one function can be called as the return value of another function. Python has made it very easy to put a function inside another function and has defined a shortcut called Decorator.
More complex tasks are related to functions and Iterables in Python. An Iterable is a collection of enumerable data in Python that can be selected from among its members. Iterable is translated by iterators or iterators.