Python:Functional Programming
Tags: beginners, python, programming, tutorial
Published: 2022-06-18 08:03:16

Defining functions
Python can also be used for functional programming as it can for structural programming. This can be done by defining the function which can be done with or without an argument.
The code inside the function won't be run unless called by the user.
An example of a basic code is this.
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vuq8eaq8yj3fj8hcfp8.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zydqb2but1plbxiwgr0m.png)
Here the function takes no argument.
An example of one which takes into an argument is:
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5hx14i9ivro3g739vxe.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u81iwvne19zo19nagvpv.png)
Functions can take in one or more arguments depending on the programmer's need. Functions are widely used in python because of their ability to be called upon anywhere in the program whenever a syntax is needed to be repeated twice or more times, which is generally better than typing the same line of code and pasting it when needed over and over which creates repetition and a rigid program.

In functional programming, there is also a return function that passes out the result of the operation performed without being printed or seen by the user, the output of the function can then be taken as the input of another function.
Here's a simple function that adds two numbers and doubles them:
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlu8pwoixev9on9ikqxh.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwx8l2u9kkfhk4gk2ok8.png)
Here's a simple example of how functions work
The above function can also be written like this:
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fhb6vf695x4gv2rvr4t.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcvgi45u09fcd3ql4dbh.png)
A simple program of adding two numbers as written before with our current knowledge is:
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg9nmf4d2ar76omvw24g.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezpv3zy1u05bojdx81cp.png)
Just like structural programming which uses iterations to run over a code multiple times, functional programming involves the use of iterations which involve calling the function inside the function and it will keep running until a base case is reached and the result is displayed.
There's a simple example of recursions.
The use of factorial (factorial  is a product of a number by the number -1 until 1 is reached, I.e 3! = 3×2×1,5!=5×4×3×2×1)
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkuu6v2fsd4xugf9rzfg.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksthgxk0rp95s094p6l0.png)
The base case here is 1.
Now let's solve a particular programming challenge:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9a3u2ar2bdc8gxilsh7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7f1g87t1qsmloff85ya.png)
_image gotten from hackerrank_

A solution:
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wj3ccy0ruuel1x1kbuha.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7skbmsaoxe3f3xo5h5lu.png)
Let me break down how I solved this
Step1:First we need an input function which will be the length of the array
Step2: I created a list comprehension to take in values entered by the user on a single line 
list=[int(x) for x in input("Enter numbers:").split()]
To explain further the list comprehension as written takes in only integers, for x in the input of the user, the .split() function collects each input removes the spaces, and collects it as an integer
Here's how it works 
Code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uws5avvoqaqas59gpnsb.png)
Output:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezeqz71l01uw373cyabg.png)
We'll learn more about that in a later article on the list and list comprehension.

Step3: I created negative positive and zero as integers which equal to zero, then created a loop to go over each of the numbers entered and group them whether they are greater than 1 equal to 0, or less than 1.

Finally; I created a function to make them all fractions by dividing the argument of the function by the length of the list and printing out the answer.
How did you solve yours, feel free to drop it in the comment below😇thanks for reading, share, and have a lovely day. We'll see more on recursions next lesson.