All posts

Functions in Python

What is a Function ?

In Python, a function is a named block of code that does a specific job. It takes input arguments (if there are), does certain operations and returns a value (if specified). Python functions allow you to modularize the code and make it more organized and re-usable.

In my words Functions in Python are used to utilize the code in more than one place in a program, sometimes also called method or procedures. Python provides you many inbuilt functions like `print()`, but it also gives freedom to create your own functions.

 

Here is a short example of a function

In the above example this function takes two arguments x and y and returns their sum using the + operator. The def keyword is used to define a function, followed by the function name, and the input arguments in parentheses. The code block is indented and contains the operations to be performed by the function. The return keyword specifies the value to be returned by the function.

 

How to write a function

From the above example it might be a bit clear on how a function looks and how it is written. Let us look at one more example of a python function.

In the above example, we have have a function that takes 1 argument x. It returns it’s square using the * operator for multiplication. In the next section we are calling the function perform the action using 5 as a parameter for the function and storing it in a variable named as result. After acquiring the result we are just printing it using a print()statement.

 

Different Types of Function

There are different types of functions in python based on the defined usage of it. I am listing down the name, some explanation and examples of the functions below

 

1. Built-in Functions: These are the functions that are predefined in Python, which means that they are available in all Python programs without the need to import plug-ins. Examples include print(), len(), input(), range(), type(), and str().

 

2. User-Defined Functions: These are functions that are created by the programmer to perform specific tasks in their program. They are defined using the def keyword, followed by the function name and the parameters in parentheses. The function block contains the operations to be performed, and the return statement specifies the value to be returned by the function.

 

3. Anonymous Functions: Also known as lambda functions, these are small, one-line functions that do not have a name. They are created using the lambda keyword and can take any number of arguments. Lambda functions are commonly used with higher-order functions such as map(), filter(), and reduce().

 

4. Recursive Functions: These are functions that call themselves in order to solve a problem. They are often used for complex problems that can be broken down into smaller subproblems. Recursive functions have a base case, which is the stopping condition, and a recursive case, which is the condition that calls the function again.

 

5. Higher-Order Functions: These are functions that take other functions as arguments or return functions as their result. Examples include map(), filter(), reduce(), and sorted(). Higher-order functions are commonly used in functional programming.

 

6. Method: Methods are functions that are bound to an object, and they can access the object’s data. They are defined in a class and are called using the dot notation, with the object as the left operand and the method as the right operand.

 

Conclusion

So this was all about functions in Python. Hope you enjoyed reading it and it was of some help to you. Hope to see you soon.