Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.4: Functional Programming

2 Replies, 1254 Views

[Image: BFMMlbcQvml9HSqXcvNp]
Functional Programming:
              Functional Programming (FP) is a method of problem solving like other methods OOP or Procedural. It includes Pure Functions that don’t include side effects in the program. It has a strict control flow meaning that it takes input as argument and returns the output directly. This style of programming is based on functions. The key part of FP is Higher-order Functions. Higher-order Function is that which takes functions as arguments, or returns them as result.
Let’s write the code for better understanding of FP. First let’s try this with the procedural method. So this program adds two numbers and returns the square of them. Let's code it.

Code:
# Imperative way

def squared_sum(x,y):
    sum = x + y
    square = sum ** 2
    return square
print(squared_sum(10,4))


This function has side effects because variables used in this case ‘sum’ and 'square’ are affecting something which in this case is memory. Side effects are events caused by the system within limited scope whose effects are felt outside of that scope. Now let’s see the above example in the Functional Programming approach.

Code:
# Functional Programming approach
def squared_sum(x,y):
    return (x + y) ** 2
print(squared_sum(10,4))

This approach helps the developer to easily understand the operations of a given system.

Now let’s see another example which includes higher-order functions.

Code:
def test(func, arg):
    return(func(func(arg)))
def mult(x):
    return x*x
print(test(mult,2))

Output: 16

focus on this :
return func(func(arg)) == mult(mult(2))
2 is passed to the function mult the result is 4 and now we are passing the result of that function again
Inside/Argument = mult(2) = 4
Outside/Returned = mult(inside) = mult(4)
Hope you get the Logic.

That’s it for today in the next thread we will talk about pure functions in detail. 


"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"

"Keep Learning and Keep exploring"
Love me like you do Heart
The first example was easy and clear to understand but the second one is a little more confusing than it should be 😂

I think the second one can't be understanded even tho this functional method should make it easer.
Rs
* Thankful to Allah *
Kurdy
(05-07-2022, 09:57 AM)Mr.Kurd Wrote: The first example was easy and clear to understand but the second one is a little more confusing than it should be 😂

I think the second one can't be understanded even tho this functional method should make it easer.
It has two pure function one is the real operation and the first function is taking the second's return value as argument argument
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,178 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,535 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,569 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,775 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,541 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,773 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,725 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,738 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,757 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,728 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)