Python Functions
Functions play an important role in Python programming language.A function is a block of code which is being executed for a certain condition .In Function, we pass data in the form of Parameters.Parameters and Arguments are some basic ways to pass data in the Function. Parameters are used to pass data when the function is declared whereas Arguments are used to pass the value when the function is called.Basically,Function is a set of reusable code in programming language which is been executed to accomplish a particular task.Def is a keyword which is used to define a function.
Types of Function
There are three types of functions in python which are as follows :
Built-In Functions
1. Built-In Functions: Built-in functions are readily available for use such as print() function,input() function,etc.
input() # to give the input to the program print() # to print the desired output
User-Defined Functions
2.User-Defined Functions:
For example: function used for adding numbers and obtaining their sum
# defining a function to add two numbers def sum(a,b) : a=8 b=6 sum=a+b print(sum) print(sum(8,6))
Lambda Functions
3.Lambda Functions: Lambda functions are a type of Anonymous functions i.e. a function without a name.In Lambda functions,we use Lambda keyword to define an Anonymous function.
# Python programming # lambda functions string ='Hello World' # lambda returns a function object print(lambda string : string)
Recursion Functions
4.Recursion Function: Recursive Function refers to calling a function within a function itself.This has the benefit of meaning that you can loop through data to reach a result.Recursive functions are used in queues and complex mathematical problems.
def recursion(a): if(a>0): result = a+recursion(a-1) print(result) else: result = 0 return result print("\n\n Recursion :") recursion(6)
In this way ,function helps us to reuse a particular set of code to accomplish a certain task.
Declaration of a Function
A function declaration introduces an identifier that designates a function and, optionally, specifies the types of the function parameters and arguments.A function declaration tells the compiler about a function name ,its type and how to call the function.
For Instance
Creating a Function
It refers to create a function using def keyword .Def is a keyword which is used to define a function.
In Python a function is defined using the def keyword:
# defining a function to add two numbers def sum(a,b) : a=8 b=6 sum=a+b print(sum)
In this way we could easily create a function using def keyword.
Calling a Function
Calling a function refers to calla function to execute the program.To call a function, use the function name followed by parenthesis:
def sum(a,b) : a=8 b=6 sum=a+b print(sum) sum(a,b)
Advantages of Functions in python
Following are the major usecases of functions in python:
- Boosts up the efficiency of code
- Decomposing complex problems into simpler pieces
- Improving clarity of the code
- Reuse of code
- Helps to obtain greater productivity