Introduction to Functional Programming

Introduction to Functional Programming

A brief introduction to functional Programming

Functional Programming also popularly known as (FP) is basically writing programs that are composed of functions. The idea is to write your codes in functions that can be invoked. Can you imagine a program that doesn't consist of anything else except functions? That is Functional Programming. Every single thing both small and big that needs to be done is done with functions. Actually, it is that you break every big task that can be broken into smaller tasks so that you can address them with functions.

Characteristics of Functional Programming

Two key characteristics of functional programming are that

  1. You can pass them as arguments to other functions and
  2. You can use them as the return value from a function. Now let's explain a little bit. But perhaps you don't understand what arguments and return values are, you can read this explanation of functions.

    1. You can pass them as arguments to other functions

    Below is an example of a function that accepts another function as an argument.
def a_number():
    return 5


def sum_of_two(first_number, second_number):
    return first_number + second_number

"""
this function accepts  the function a_number() 
as an argument
"""
answer = sum_of_two(a_number(), 32)

print(answer)

In the above program, the function with the name sum_of_two is accepting the return value of the function a_number as an argument.

2. You can use them as the return value from a function

If you study the program below, you will see a good example of a function that uses another function as a return value.

def sum_of_two(first_number, second_number):
    return first_number + second_number


def add_two_numbers(first, second):
    return sum_of_two(first, second)


result = add_two_numbers(5, 9)

print(result)

The function add_two_numbers returns the function sum_of_two as a the return value. If you trace it, it means that if add_two_numbers is returning the function sum_of_two, it means that the final return value will be this statement first_number + second_number which belong.

Let's now look at a different example. In this example, we are assigning the function to the variable. And this variable that has been assigned the function can be called. It is now a caller of that function.

def print_my_name():
    print("Oluwatosin Joseph Durodola")


var_test = print_my_name

var_test()

The variable with the name var_test contains the function print_my_name. If the variable is called, it will execute the code that is in the function. If you execute the above program, the output will be

Oluwatosin Joseph Durodola

The output actually belongs to the function print_my_name.

For better understanding, let us look at what is happening in the computer memory. The python interpreter is look that the variable var_test and the function print_name as the same thing. The two functions are pointing to the same location in the computer memory. How do I know this? Let us use the console to see it better.

two_functions.png

If you call the two functions in the above program without adding the parenthesis, you will notice that they have that the two of them have the same name and numbers function print_name at 0x7fc1731a2ca0. This shows that they are pointing to the same memory location in the computer. Another thing you will notice is that they have the same class, class 'function'.

Key things to understand in Functional Programming

Please note that in functional programming, we try to treat each function as a Mathematical function. The long and short of it is that we try to ensure that a function must have an input and an output.

Advantages of Functional Programming

These advantages have been copied from Freecodecamp.org

  1. It helps us to solve problems effectively in a simpler way.
  2. It improves modularity.
  3. It allows us to implement lambda calculus in our program to solve complex problems.
  4. Some programming languages support nested functions which improve the maintainability of the code.
  5. It reduces complex problems into simple pieces.
  6. It improves the productivity of the developer.
  7. It helps us to debug the code quickly.

Thanks for going through my article.