Index
Functions in Python
Built-in Functions
User-Defined Functions
return Keyword
Default Parameter in Functions
Recursion
1. Functions in Python
Definition:
A function in Python is a block of reusable code that performs a specific task. Functions help in breaking down complex problems into smaller, manageable tasks, and they promote code reuse.
Use-Case Example:
Functions are commonly used to perform repetitive tasks, such as calculating the sum of numbers, processing data, or performing mathematical operations.
Syntax:
def function_name(parameters):
# Code block
return result
Rules:
Functions are defined using the
def
keyword, followed by the function name and parentheses.Parameters (if any) are passed inside the parentheses.
The function can return a value using the
return
statement.
Basic Example:
def greet(name):
return "Hello, " + name
# Example usage
print(greet("Vishal")) # Output: Hello, Vishal
2. Built-in Functions
Definition:
Built-in functions are pre-defined functions provided by Python, which can be used directly without any need for prior definition.
Use-Case Example:
Built-in functions like print()
, len()
, and type()
are commonly used to perform everyday tasks such as displaying output, getting the length of a string, or checking the type of a variable.
Syntax:
# Example of built-in functions
print("Hello, World!") # Output: Hello, World!
length = len("Python") # Output: 6
data_type = type(123) # Output: <class 'int'>
Rules:
Built-in functions are readily available and require no import.
The function names should not be redefined to avoid conflicts.
Basic Example:
# Using built-in functions
numbers = [1, 2, 3, 4, 5]
print(len(numbers)) # Output: 5
print(sum(numbers)) # Output: 15
3. User-Defined Functions
Definition:
User-defined functions are functions created by the user to perform specific tasks. These functions are defined using the def
keyword and can accept parameters, return values, and contain any logic required by the user.
Use-Case Example:
Creating a function to calculate the area of a rectangle based on its length and width.
Syntax:
def function_name(parameters):
# Code block
return result
Rules:
Functions must be defined before they are called.
The function name should be descriptive and follow naming conventions.
The
return
statement is optional; if omitted, the function returnsNone
by default.
Basic Example:
def calculate_area(length, width):
return length * width
# Example usage
area = calculate_area(5, 3)
print(area) # Output: 15
4. return Keyword
Definition:
The return
keyword is used in a function to send the function’s result back to the caller. It exits the function and optionally passes back a value.
Use-Case Example:
Returning the result of a mathematical operation, such as the sum of two numbers.
Syntax:
def function_name(parameters):
# Code block
return value
Rules:
The
return
statement immediately exits the function.If
return
is omitted, the function returnsNone
.
Basic Example:
def add_numbers(a, b):
return a + b
# Example usage
result = add_numbers(10, 20)
print(result) # Output: 30
5. Default Parameter in Functions
Definition:
Default parameters in functions allow you to specify default values for parameters, making them optional when the function is called.
Use-Case Example:
Using default parameters to create a greeting function that defaults to "Hello" if no specific greeting is provided.
Syntax:
def function_name(param1=default_value):
# Code block
Copy code
def function_name(param1=default_value): # Code block
Rules:
Default parameters should be placed after non-default parameters.
If a value is provided during the function call, it overrides the default value.
Basic Example:
def greet(name, greeting="Hello"):
return f"{greeting}, {name}"
# Example usage
print(greet("Vishal")) # Output: Hello, Vishal
print(greet("Vishal", "Welcome")) # Output: Welcome, Vishal
6. Recursion
Definition:
Recursion is a programming technique where a function calls itself to solve a problem. It's commonly used for tasks that can be divided into similar sub-tasks.
Use-Case Example:
Calculating the factorial of a number, where n!
is the product of all positive integers up to n
.
Syntax:
def recursive_function(parameters):
if base_condition:
return result
else:
return recursive_function(modified_parameters)
Rules:
A base condition is necessary to avoid infinite recursion.
Recursion can be memory-intensive and should be used with caution.
Basic Example:
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n - 1)
# Example usage
print(factorial(5)) # Output: 120