Function
- The function is the set of instructions to perform a specific task, nothing but functions.
- A subprogram or a smaller part of the main program.
- A block of code that performs a specific task and can be called when needed.
- Perform specific operations.
- Promote code reusability, allowing the same block of code to be used multiple times without rewriting.
Using functions in programming provides several benefits:
- Reduced Development Time: Modular code is faster to write and maintain.
- Lower Memory Usage: Reusing functions avoids duplicating code.
- Faster Execution: Optimized, reusable code can improve performance.
- Improved Performance: Modular design enhances application efficiency.
- Minimized Code Redundancy: Avoids repetitive code, making programs cleaner and easier to maintain.
Functions in Python consist of two main components:
- Function Definition: Defines the function’s name, parameters, and the code block to execute.Created using the def keyword.Exists only once in the program.
- Defines the function’s name, parameters, and the code block to execute.
- To create function using the def keyword.
- Exists only once in the program.
- Function Calls: Invokes the function to execute its code.Can occur multiple times in the program.Requires a corresponding function definition, or a NameError will occur.
- Invokes the function to execute its code.
- Can occur multiple times in the program.
- Requires a corresponding function definition, or a NameError will occur.
Every function typically involves three phases:
- Input: Accepts data (parameters or arguments) to process.
- Process: Performs operations on the input data.
- Output: Returns a result or produces an effect (e.g., printing, modifying data).
In Python, functions can be categorized into several types based on their characteristics and how they are defined. Here are the main types of functions in Python:
These are functions that come pre-defined with Python and are available for use without any imports. Examples include:
-
print()
-
len()
-
type()
-
sum()
-
min()
-
max()
These are functions that you define yourself using the def
keyword. For example:
Also known as anonymous functions, these are small, one-line functions defined using the lambda
keyword. They do not require a name and can have any number of input parameters but only one expression.
A function that calls itself in order to solve a problem. It's often used in problems involving recursion, like calculating factorials or performing tree/graph traversals.
These are functions that take one or more functions as arguments, or return a function as a result. Common examples include map()
, filter()
, and reduce()
from the functools
module.
These functions have default values for some of their parameters, which are used if no argument is provided for those parameters.
Functions that can accept a variable number of arguments:
-
*args
: Used to pass a variable number of non-keyword arguments. -
**kwargs
: Used to pass a variable number of keyword arguments.
Example with *args
:
Example with **kwargs
:
While lambda functions are user-defined, they can also be used directly within other functions like map()
, filter()
, and sorted()
to apply functions to sequences.
These are functions that "freeze" some portion of the function’s arguments, producing a new function. This can be done using functools.partial()
.
Each type of function serves a specific purpose and can be chosen based on the use case.