0% found this document useful (0 votes)
3 views6 pages

FUNCTIONS

This document provides an overview of functions in Python, detailing how to define, call, and utilize functions with parameters, return values, default parameters, keyword arguments, and variable-length arguments. It also introduces lambda functions and provides examples for each concept. Overall, it emphasizes the modularity and reusability of code through the use of functions.

Uploaded by

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

FUNCTIONS

This document provides an overview of functions in Python, detailing how to define, call, and utilize functions with parameters, return values, default parameters, keyword arguments, and variable-length arguments. It also introduces lambda functions and provides examples for each concept. Overall, it emphasizes the modularity and reusability of code through the use of functions.

Uploaded by

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

FUNCTIONS

Functions in Python are reusable blocks of code that perform a specific task.
They help in organizing the code, making it more modular, readable, and
maintainable. Here’s an overview of functions in Python:

1. Defining a Function
You define a function using the def keyword followed by the function name,
parentheses, and a colon. The function body is indented.

def greet():
print("Hello, World!")

● This defines a simple function named greet that prints "Hello, World!".

2. Calling a Function
You call a function by using its name followed by parentheses.

greet()

● This calls the greet function and prints "Hello, World!".

3. Function with Parameters


Functions can take parameters (or arguments) to work with different values.

def greet(name):
print(f"Hello, {name}!")

greet("Alice")
greet("Bob")
● This defines a function greet that takes one parameter name and prints a
personalized greeting.

4. Function with Return Value


Functions can return a value using the return statement.

def add(a, b):


return a + b

result = add(3, 5)
print(result) # Output: 8

● This defines a function add that takes two parameters, adds them, and
returns the result.

5. Default Parameters
You can provide default values for parameters, making them optional.

def greet(name="World"):
print(f"Hello, {name}!")

greet() # Output: Hello, World!


greet("Alice") # Output: Hello, Alice!

● This defines a function greet with a default parameter value "World".

6. Keyword Arguments
You can call functions using keyword arguments, specifying parameter names
explicitly.

def greet(first_name, last_name):


print(f"Hello, {first_name} {last_name}!")

greet(first_name="Alice", last_name="Smith")
greet(last_name="Doe", first_name="John")

● This calls the greet function using keyword arguments.

7. Variable-Length Arguments
Variable-length arguments in Python allow you to pass a variable number of
arguments to a function. This is done using *args for positional arguments and
**kwargs for keyword arguments.

1. Using *args (Positional Variable-Length Arguments)

*args allows you to pass a variable number of positional arguments to a


function. Inside the function, args is a tuple of the passed arguments.
Example:

def greet(*names):

for name in names:

print(f"Hello, {name}!")

greet("Alice", "Bob", "Charlie")

In this example:

The greet function can take any number of arguments.

names is a tuple containing all the arguments passed to the function.

The function prints a greeting for each name.


2. Using **kwargs (Keyword Variable-Length Arguments)

**kwargs allows you to pass a variable number of keyword arguments to a


function. Inside the function, kwargs is a dictionary of the passed arguments.
Example:

def print_details(**details):

for key, value in details.items():

print(f"{key}: {value}")

print_details(name="Alice", age=30, city="Wonderland")

In this example:

The print_details function can take any number of keyword arguments.

details is a dictionary containing all the keyword arguments passed to the


function.

The function prints each key-value pair.

3.Combining *args and **kwargs

You can combine *args and **kwargs in a single function to handle both types
of variable-length arguments.
Example:

def mix_function(*args, **kwargs):

print("Positional arguments:")

for arg in args:


print(arg)

print("\nKeyword arguments:")

for key, value in kwargs.items():

print(f"{key}: {value}")

mix_function(1, 2, 3, name="Alice", age=30, city="Wonderland")

In this example:

mix_function can take both positional and keyword arguments.

args is a tuple of positional arguments.

kwargs is a dictionary of keyword arguments.

Example of Using Variable-Length Arguments:

def calculate_total(*prices, discount=0):

total = sum(prices)

total -= total * (discount / 100)

return total

print(calculate_total(100, 200, 300, discount=10)) # Output: 540.0


8. Lambda Functions
Lambda functions are anonymous functions defined using the lambda keyword.

Syntax

lambda arguments: expression

Example

add = lambda a, b: a + b
print(add(3, 5)) # Output: 8

● This defines a lambda function that adds two numbers.

Example of Using Functions:

def calculate_area(radius):
return 3.14 * radius * radius

r=5
area = calculate_area(r)
print(f"The area of a circle with radius {r} is {area}")

You might also like