![]() |
What is Function in Python? |
What is Function in Python?
A Function in python is a block of code that is run whenever called in your code.
You can pass The data which is called as parameter
There are Two Types of function
- User Define Function : that is define by user
- Built-In Function : Built-in function created by default like print(),input() etc
1) User Define Function
User Define Function in python ,that is created by user for run specific task , that is Executed whenever you called in your code
User Define Function is created using " def " Keyword
Example
# create a Funcction
def MyFun() :
print("Hello Everyone")
# call a function
MyFun()
Output
Hello Everyone
User define function with parameters
Example
# create a Funcction
def MyFun(name) :
print("Hello ", name)
# call a function
MyFun("Jack")
MyFun("John")
Output
Hello Jack
Hello John
2) Built-In Function
Built-In Function in python , a Function that is by default available in python programming language
There are many built-in Function Python , like print(),len(),input() etc
Example
# here how we are create built in len() function and example
# len() function is used for Return the length
lst = ["dog","lion","tiger"]
x = len(lst)
Output
3
Advantages
- Reuse code
- Improve code clarity
- Reducing duplication of code
- Information hiding