0% found this document useful (0 votes)
15 views8 pages

Functions - 22-10-2024

Uploaded by

gakani7830
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Functions - 22-10-2024

Uploaded by

gakani7830
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Functions

D.HEMILA REXLINE
SCOPE
Function Composition in Python
Function composition combines two functions in such a way that the result of one function is passed as an
argument to the other function. It can be denoted as f(g(x)) where, x is an argument to the function g() and
the result of g(x) is an argument to the function f(). The result of function composition is finally given as the
result of function f().
def add(x, value):
return x + value
Example:
def multiply(x, factor):
return x * factor
#Syntax def compose(f, g):
def compose(f, g): def composed_function(x, value, factor):
def composed_function(x): return f(g(x, value), factor)
return f(g(x)) return composed_function
return composed_function # Create a composed function
composed_func = compose(multiply, add)

# Use the composed function


result = composed_func(10, 5, 2)
print(result) # Output: (10 + 5) * 2 = 30
Documentation Strings
• It provide a way to describe what a piece of code does, how it works, and how to use it.
• Docstrings are written as the first statement inside a function, class, or module and are
enclosed in triple quotes (""" """ or ''' '''), which allows for multiline strings.
• Documentation strings (often called docstrings) are used to document the purpose,
behavior, and usage of modules, classes, functions, and methods.
• Docstrings (documentation strings) serve the same purpose as that of comments, as
they are designed to explain code. However, they are more specific and have a proper
syntax.
• Placement: Docstrings must be the first statement in a function, class, or
module.
• Accessing Docstrings: They can be accessed using the .__doc__
attribute.
• Purpose: Docstrings are essential for creating well-documented and
maintainable code, especially in large projects or shared codebases.

Example
def add_numbers(a, b):
"""
Adds two numbers together.

Parameters:
a (int or float): The first number.
b (int or float): The second number.

Returns:
int or float: The sum of a and b.

Example:
>>> add_numbers(2, 3)
5
"""
return a + b

# Accessing the docstring


print(add_numbers.__doc__)
Recursive Functions
Recursive function is a function that calls itself in order to solve a problem. Recursive
functions break down complex problems into simpler versions of themselves, and they
generally have two main components:
1. Base case: The condition under which the recursion stops. It prevents the function from
calling itself indefinitely.
2. Recursive case: The part where the function calls itself with modified arguments, bringing
the problem closer to the base case.
Recursive Function to Calculate Factorial:
def factorial(n):
""" factorial(5) = 5 * factorial(4)
Recursive function to calculate the factorial of a number. factorial(4) = 4 * factorial(3)
factorial(3) = 3 * factorial(2)
Parameters: factorial(2) = 2 * factorial(1)
n (int): The number to calculate the factorial of. factorial(1) = 1 # Base case

Returns:
int: The factorial of n.
"""
if n == 1 or n == 0: # Base case
return 1
else:
return n * factorial(n - 1) # Recursive case

# Example usage:
result = factorial(5)
print(factorial.__doc__)
print("Factorial of 5 is:", result)
•Base case: If n is 0 or 1, return 1 (since 0!=1!=1).
•Recursive case: Multiply n by the factorial of n−1. The recursion continues
until the base case is reached.
Advantages of Recursive Functions:

• Simplifies Complex Problems: Problems that can be divided into similar


sub problems, like tree traversal, searching algorithms, and sorting
algorithms, can be more intuitively solved using recursion.

• Code Readability: Recursive code is often more elegant and easier to


understand for problems that naturally fit the recursive pattern.

You might also like