PYTHON — Functions
Functions are a fundamental concept in programming that allows to organize code into reusable blocks. In Python, functions play a crucial role in making code modular, readable, and maintainable. In this article, I will go through different types of functions in Python, including examples and codes, and explore various types of arguments used in function definitions.
What is Function?
Function is a block of code which executes only when it is called. Function is used for the re-usability of the code and minimize redundancy i.e. repeating the same code again and again.
Steps involved in a function:
→ Define a function.
→ Call a function using function name.
Types of Functions in Python:
i. Built-in Function
ii. User-defined Function
Built-in Function
Built-in functions are the predefined functions that are already present or available in python. Example — print(), len(),split(), etc.
# Example of a built-in function
print("Hello, world!")User-defined Function
User-defined functions are the functions that are defined by the user to perform some specific tasks in python. They enhance code readability and reusability.
# Example of a user-defined function
def greet():
print('Hello')
print('Good Morning')
greet() Output:
Hello
Good MorningArgument
Arguments are the elements that is mentioned inside the function.
Types of Arguments in Python
1. Position Argument
2. Keyword Argument
3. Default Argument
4. Variable length Argument
5. Keyword variable length argument
1. Position Argument
Get NIRAJAN JHA’s stories in your inbox
Join Medium for free to get updates from this writer.
These are the arguments passed to a function based on their position in the function call. In position argument, position or order matters. It is important to note that position argument should be used only when position of argument is known.
def name(first_name,last_name):
print(first_name,last_name)
name('John','Lewis') Output:
John Lewis2. Keyword Argument
These are the arguments passed to a function with their corresponding parameter names .In keyword argument, keyword or keyname is accepted. In this, position does not matter.
def name(first_name,last_name):
print('first name= ',first_name,'\nlast name= ',last_name)
name(first_name = 'John',last_name = 'Lewis')Output:
first name = John
last name = Lewis3. Default Argument
These are the arguments with default values specified in the function definition. In this, one of the arguments is already assigned in function definition. So, even if you don’t mention that argument while calling the function, it will print the output with that value by default.
def name(first_name,last_name = 'Lewis'):
print('first name= ',first_name,'\nlast name= ',last_name)
name(first_name = 'John')Output:
first name = John
last name = Lewis4. Variable length Argument
These are the arguments in which multiple number of arguments can be provided in the function definition. In this, data is stored in tuple format. It is represented by a single ‘*’.
def add(*a):
total = 0
for i in a:
total+=i
print(total)
add(12,4,4,15)Output:
355. Keyword variable length argument
These are the arguments in which multiple number of arguments with keywords or keynames can be provided in the function definition. In this, data is stored in dictionary format. It is represented by double ‘*’.
def info(**data):
for i,j in data.items():
print(i,'=',j)
info(Name = 'John Lewis',Age = 25,Location ='London')Output:
Name = John Lewis
Age = 25
Location = LondonTo know about Lambda Function and its application with codes:
You can Click Here.
Conclusion
Understanding different types of functions and arguments enables developers to write efficient and readability code. Whether using built-in functions or creating user-defined functions, Python’s versatility in handling functions makes it a powerful language for developers.




![15 Common SQL Coding Interview Questions and Answers [2024]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*IFMJeeZ22BnUSYS-JECFXw.png)



