Python Functions

Carson West

Function Parameters

Python Functions

These notes cover the basics of Python functions.

Defining Functions:

Functions are defined using the def keyword, followed by the function name, parentheses (), and a colon :. The function body is indented.

def my_function(param1, param2):
  """This is a docstring describing the function."""
  # Function body
  result = param1 + param2
  return result

Function Parameters and Arguments

Function Return Values

Calling Functions:

Functions are called by using their name followed by parentheses containing any necessary arguments.

result = my_function(5, 3)  # Calls my_function with arguments 5 and 3
print(result)  # Prints 8

Types of Arguments:

Scope and Lifetime of Variables:

Lambda Functions (Anonymous Functions):

These are small, anonymous functions defined using the lambda keyword. They are often used for short, simple operations.

square = lambda x: x*x
print(square(5)) # Prints 25

Recursive Functions:

Functions that call themselves. They must have a base case to avoid infinite Recursion. Recursion in Python

Decorators:

A powerful technique for modifying or enhancing functions without changing their core functionality. They use the @ symbol. Python Decorators

Higher-Order Functions:

Functions that take other functions as arguments or return functions as results. map, filter, and reduce are examples. Higher-Order Functions

Pre-existing Notes: