top of page

Python Functions

In Python, a function is a block of reusable code that performs a specific task. Functions are used to organize code, improve readability, and promote code reusability. Here's an overview of how functions work in Python:


Defining a Function:

To define a function in Python, you use the `def` keyword followed by the function name, parentheses `( )`, and a colon `:`. You can also specify parameters (inputs) inside the parentheses if the function requires any.


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

def add_numbers(a, b):
    return a + b

Calling a Function:

To execute a function, you call it by its name followed by parentheses `( )`. If the function expects parameters, you pass them inside the parentheses.



greet()  # Output: Hello, there!

result = add_numbers(2, 3)
print(result)  # Output: 5

Returning Values:

Functions can return values using the `return` statement. The value returned can be stored in a variable or used directly.



def multiply(a, b):
    return a * b

product = multiply(4, 5)
print(product)  # Output: 20

Function Parameters:

You can pass values to functions through parameters. Python supports two types of function parameters: positional parameters and keyword parameters.


Positional Parameters: These parameters are defined in the function header and receive values in the order they are passed.



def greet(name, age):
    print(f"Hello, {name}! You are {age} years old.")

greet("Alice", 25)  # Output: Hello, Alice! You are 25 years old.

Keyword Parameters: These parameters are specified with a default value in the function definition. They can be assigned values by name when calling the function.



def greet(name="Anonymous", age=30):
    print(f"Hello, {name}! You are {age} years old.")

greet(age=35)  # Output: Hello, Anonymous! You are 35 years old.

Note that keyword parameters are optional and can be omitted when calling the function.


Scope of Variables:

Variables defined inside a function have local scope, meaning they can only be accessed within the function. Variables defined outside functions have global scope and can be accessed both inside and outside functions.



def print_message():
    message = "Hello, there!"
    print(message)

print_message()  # Output: Hello, there!
print(message)   # NameError: name 'message' is not defined

In the above example, the `message` variable is only accessible within the `print_message()` function.


These are the basic concepts of Python functions. They allow you to encapsulate code, reuse it, and make your programs more modular and efficient.

Related Posts

See All

Mastering Lists in Python

Introduction Lists are one of the most fundamental and versatile data structures in Python. They allow you to store and manipulate collections of items, making them an essential tool for any Python pr

Python Anonymous Function

In Python, an anonymous function is a function that is defined without a name. It is also known as a lambda function because it is created using the `lambda` keyword. Anonymous functions are typically

Comments


bottom of page