What is a Function in Python? | Python Functions Explained with Examples
A function is a block of reusable code that performs a specific task . function help to organize code , improve readability and avoid repetition .
How to define Function in python
you define a function using the def keyword , parentheses ()
can hold parameters . this makes your code cleaner and easier to manage—especially when you need to repeat the same logic in multiple places.
def welcome(name)
return f"Hello, {name}"

How to call Function in python
Calling a function in Python is pretty straightforward! Once you’ve defined a function using the def
keyword, you just need to write its name followed by parentheses. If the function takes arguments, you pass them inside the parentheses.
# Define the function
def say_hello(name):
print(f"Hello, {name}!")
# Call the function
say_hello("Alice")
Types of function in python
- function with no parameter
- function with parameter
- function with default parameters
- function with variable arguments
- function with keyword argument
- lambda function
- function with return value
- recursive function
1. Function with no parameters
A function with no parameter in Python is one that doesn’t require any input when it’s called. It simply performs an action or returns a value without needing any external information.
def welcome_message():
print("welcome to python function programming")
#calling the function
welcome_message()
2. Function with parameters
A function with parameters in Python is like a reusable block of code that expects certain information to be passed in when it’s called
def say_hello(name):
print(f"Hello, {name}!")
say_hello("Rakesh")
3.function with default parameters
Functions can have default parameters, which means you can assign default values to arguments so that the function can be called with fewer arguments than it is defined to accept.
def power(base, exponent=2):
return base**exponent
print(power(3))
print(power(3,3))
4.function with variable arguments
function with variable argument used when you don’t know how many arguments will be passed . In Python functions with different types of variable arguments .
These arguments take default values if no value is provided during the function call.
def say_hello(name="Guest"):
print(f"Hello, {name}!")
say_hello() # Output: Hello, Guest!
say_hello("Rakesh") # Output: Hello, Rakesh!
*args
– Non-keyword variable arguments
- Captures positional arguments.
- Stored as a tuple.
- You use it when you want to accept a variable number of positional values.
def add_numbers(*args):
print(args)
add_numbers(10, 20, 30)
# Output: (10, 20, 30)
**kwargs
– Keyword variable arguments
- Captures keyword arguments .
- Stored as a dictionary.
- You use it when you want to accept a variable number of named values.
def print_details(**kwargs):
print(kwargs)
print_details(name="Aria", age=25)
# Output: {'name': 'Aria', 'age': 25}
5.Lambda(anonymous) function
A lambda function in Python is a small, anonymous function defined using the lambda
keyword. It’s typically used when you need a short function for a short period and don’t want to formally define it using def
.
Syntax :
lambda arguments: expression
Example:
# A lambda function that adds 10 to the input
add_ten = lambda x: x + 10
print(add_ten(5)) # Output: 15
# A lambda function that multiplies two numbers
multiply = lambda a, b: a * b
print(multiply(4, 6)) # Output: 24
6.Lambda(anonymous) function
A function is a reusable block of code that performs a specific task. When a function has a return value, it means it sends back a result to the part of the program where it was called. This is done using the return
statement.
Syntax:
def function_name(parameters):
# do something
return result
Example
def add(a, b):
result = a + b
return result
sum_result = add(3, 5)
print(sum_result) # Output: 8
7.Recursive function
A recursive function that calls itself to solve smaller instances of a problem until it reaches a base case that stops the recursion.
Example
def factorial(n):
if n == 0: # Base case
return 1
else: # Recursive case
return n * factorial(n - 1)
print(factorial(5)) # Output: 120