DEV Community

VINOTH
VINOTH

Posted on

Functions,Arguments,Module in Python

Functions are reusable blocks of code, Arguments are the actual data inputs passed into them, Modules are files containing Python code used to organize projects.

1. Functions:
A Python Function is a self-contained block of code designed to perform a specific task. Functions help break large programs into smaller, organized, and reusable parts to prevent repetitive code.

# Defining a function
def Tech():
    return "Hello, welcome back!"

# Calling the function
print(Tech())
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • def is the keyword used to define a function.
  • Tech is the function name.
  • () indicates that the function takes no arguments.
  • return sends the result back to the caller.
  • print(Tech()) calls the function and displays the returned value.

image

2. Arguments:

  • Arguments are the concrete values you pass into a function when calling it.
  • (Note: Parameters are the variable names inside the function definition, while arguments are the real values mapped to them).
    image

  • Positional Arguments: Assigned based strictly on the order they are passed.

def greet(name):
    print(name)

greet("Vinoth")
Enter fullscreen mode Exit fullscreen mode
  • Keyword Arguments: Passed explicitly using the parameter_name=value format, meaning order does not matter.
def student(name, age):
    print(name, age)

student(age=21, name="Vinoth")
Enter fullscreen mode Exit fullscreen mode
  • Default Arguments: Fallback values defined in the function signature if an argument is missing during the call.
def greet(name="Guest"):
    print(name)

greet()
greet("Vinoth")
Enter fullscreen mode Exit fullscreen mode
  • Arbitrary Arguments (*args and **kwargs): Allow a function to accept a dynamic, flexible number of inputs.
def total(*numbers):
    print(sum(numbers))

total(10, 20, 30, 40)
Enter fullscreen mode Exit fullscreen mode

3. Modules:
A Module is simply a file containing Python statements and definitions (ending in .py) that you can import into other scripts. Modules let you logically organize your code base and share functionality across different files.
image

  • Built-in Modules: Python comes pre-packaged with useful standard modules like math, random, or datetime.
  • Custom Modules: You can create your own file (e.g., mymodule.py) and import it elsewhere using import mymodule.

Top comments (0)