Course Content
How to Add a Virtual Environment in Python
0/1
Set
0/1
Python
About Lesson

Functions in Python are reusable blocks of code that perform a specific task. They help organize code, reduce repetition, and improve readability.

You define a function using the def keyword, followed by the function name and parentheses. Any parameters the function takes are listed inside the parentheses.

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

To use the function, you call it by its name and provide any necessary arguments.

Python
greet("Guys")  # Output: Hello, Guys!

Default Parameters

You can assign default values to parameters.

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

greet()        # Output: Hello, World!
greet("Sheetal Mathur")  # Output: Hello, Sheetal Mathur!

Return Statement

Functions can return values using the return keyword.

Python
def add(a, b):
    return a + b

result = add(3, 4)  # result is now 7

Parameters and Arguments

Parameters are the variables listed in a function’s definition.

Arguments are the values you pass to the function when you call it.