🧩 Python Functions — Reusable Blocks of Code

Introduction 🌟

A function in Python is a reusable block of code that performs a specific task. Functions help make programs more organized, readable, and efficient.

Note

💡 Define once → Use many times. Functions help avoid repeating code.

1. Defining a Function 🧱

define_function.py

def greet():
    print("Hello, welcome to Python!")

2. Calling a Function 📞

call_function.py

greet()

âœ”ī¸ The parentheses () are required to execute a function.

3. Function With Parameters đŸŽ¯

function_params.py

def greet(name):
    print("Hello", name)

greet("Sathish")

Note

âœ”ī¸ Parameters allow input values to be passed into the function.

4. Function With Multiple Parameters ➕

multiple_params.py

def add(a, b):
    print(a + b)

add(10, 20)

5. Return Statement â†Šī¸

return_statement.py

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

result = add(10, 5)
print(result)

Note

âœ”ī¸ return sends a value back to the caller.
âœ”ī¸ Without return, a function gives None.

6. Default Parameters 🧃

default_params.py

def greet(name="Guest"):
    print("Hello", name)

greet()
greet("Kumar")

âœ”ī¸ If no argument is given, default value is used.

7. Keyword Arguments đŸ—‚ī¸

keyword_args.py

def student(name, age):
    print(name, age)

student(age=21, name="Sathish")

Note

âœ”ī¸ Order doesn’t matter when using keyword arguments.

8. Arbitrary Arguments (*args) đŸ”ĸ

*args allows any number of positional arguments.

args_example.py

def add(*nums):
    print(sum(nums))

add(1, 2, 3, 4)

9. Arbitrary Keyword Arguments (**kwargs) 🧰

**kwargs allows passing multiple named arguments.

kwargs_example.py

def display(**info):
    print(info)

display(name="Sathish", age=25)

10. Return Multiple Values 🎁

multiple_return.py

def calc(a, b):
    return a + b, a - b, a * b

x, y, z = calc(10, 5)
print(x, y, z)

11. Nested Functions đŸĒœ

nested_function.py

def outer():
    print("Outer function")

    def inner():
        print("Inner function")

    inner()

outer()

12. Lambda Functions ⚡

Lambda functions are small, anonymous functions written in one line.

lambda_example.py

square = lambda x: x * x
print(square(5))

13. Docstrings 📝

docstring.py

def add(a, b):
    """This function returns the sum of two numbers."""
    return a + b

14. Scope of Variables 🌐

Local Scope

local_scope.py

def func():
    x = 10   # local variable
    print(x)

func()

Global Scope

global_scope.py

x = 20  # global variable

def func():
    print(x)

func()

15. Using global Keyword 🌍

global_keyword.py

count = 0

def increment():
    global count
    count += 1

increment()
print(count)

Note

âš ī¸ Use global carefully — can make code harder to maintain.

16. Pass Statement in Functions 🚧

pass_function.py

def todo():
    pass  # function not implemented yet

17. Real-World Examples 🌍

Login Function

login_example.py

def authenticate(username, password):
    if username == "admin" and password == "1234":
        return "Access Granted"
    return "Access Denied"

print(authenticate("admin", "1234"))

Calculator Function

calculator.py

def calc(a, b, op):
    if op == "+":
        return a + b
    elif op == "-":
        return a - b
    elif op == "*":
        return a * b
    elif op == "/":
        return a / b

print(calc(10, 5, "*"))

Reusable Greeting Function

greeting.py

def greet_user(name):
    return f"Welcome, {name}!"

print(greet_user("Kumar"))

Conclusion 🎉

>>“Functions make your code modular, reusable, and clean — the foundation of good programming.” ✨

You now have a complete understanding of Python Functions! Want the next tutorial? Try Arguments & Parameters, Lambda Functions, Modules, or OOP Concepts. Just tell me! 😊