You are on page 1of 3

Python Functions

Defining Your Own Functions

A function is a block of organized, reusable code that is used to perform a specific task.
Functions are essential for writing modular and efficient code. They help in breaking down
complex tasks into smaller, manageable chunks, making code easier to read, understand,
and maintain.

To define a function in Python, you use the def keyword followed by the function name,
parentheses for any arguments, a colon, and the function body indented by one or four
spaces. The function body contains the statements that will be executed when the function
is called.

Calling Functions

Once a function is defined, it can be called using its name followed by parentheses. If the
function takes arguments, you pass the values enclosed in parentheses. For example, if you
have a function called greet() that takes a name as an argument, you would call it like this:

Python
greet("Alice")

Passing Parameters and Arguments

Parameters are the variables defined in the function definition that hold the values passed
when the function is called. Arguments are the actual values passed to the function when it
is called.

For example, in the greet() function, name is the parameter, and "Alice" is the argument.
When you call greet("Alice"), the value "Alice" is passed to the function and stored in the
name parameter.

Python Function Arguments

Function arguments can be positional or keyword arguments. Positional arguments are


passed in the order they appear in the function definition, while keyword arguments are
passed by name using the keyword argument syntax: parameter_name=argument.
Anonymous Functions (Lambda Functions)

Lambda functions, also known as anonymous functions, are concise functions defined
without using the def keyword. They are typically used for small, one-line tasks.

For example, a lambda function to calculate the square of a number can be written as:

Python
square = lambda x: x * x

Fruitful Functions (Function Returning Values)

Functions can return values using the return statement. The returned value can be used by
the code that called the function.

For example, a function to calculate the factorial of a number can be written as:

Python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

Scope of Variables in a Function

The scope of a variable refers to the part of the code where the variable is accessible.
Variables defined inside a function are local to that function and cannot be accessed directly
from outside the function.

Powerful Lambda Functions in Python

Lambda functions are powerful tools in Python due to their conciseness and ability to handle
anonymous functions. They are often used in map, filter, and reduce operations, making
them essential for functional programming in Python.

Example of using lambda functions in map:


Python
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers)

This code squares each number in the numbers list and stores the results in the
squared_numbers list.

You might also like