Functions | Dataplexa

Functions in Python

Functions are one of the most important building blocks of any programming language. They help break large programs into smaller, manageable pieces, making code cleaner, reusable, and easier to understand. In Python, functions are extremely flexible and beginner-friendly, which is why they are used everywhere — from simple scripts to advanced applications.


What Is a Function?

A function is a block of code that performs a specific task. Instead of writing the same code again and again, you define a function once and use it whenever needed. This improves readability and reduces errors.

Simple definition:

A function is a reusable piece of code that runs only when it is called.


Defining a Function in Python

Python uses the def keyword to define a function.

def greet():
    print("Welcome to Dataplexa!")

The above function does nothing until we “call” it.


Calling a Function

To execute a function, simply write its name followed by parentheses.

greet()

Functions with Parameters

Parameters allow functions to accept information from the user. This makes functions dynamic and more useful.

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

greet("Sree")
greet("Alex")

Here, name is a parameter. When the function is called, we pass an argument to fill that parameter.


Multiple Parameters

Functions can take more than one parameter by separating them with commas.

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

add(10, 20)
add(55, 45)

Return Statement

Instead of printing the result, functions can return a value. This is common in real applications because the returned value can be reused elsewhere.

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

result = multiply(6, 7)
print("Result:", result)

A function ends immediately after returning a value.


Default Parameters

Sometimes we want a function to have default values if the user does not provide one. This prevents errors and simplifies calling the function.

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

greet()
greet("Sree")

Keyword Arguments

You can specify arguments using their parameter names. This improves clarity and avoids confusion with order.

def student(name, age):
    print("Name:", name)
    print("Age:", age)

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

Variable-Length Arguments (*args)

If you don’t know how many values will be passed to a function, use *args. It collects unlimited arguments into a tuple.

def total(*numbers):
    print(numbers)
    print("Sum:", sum(numbers))

total(10, 20, 30)
total(5, 15)

Variable-Length Keyword Arguments (**kwargs)

**kwargs allows passing any number of keyword arguments. It stores values in a dictionary.

def details(**info):
    print(info)

details(name="Sree", age=22, country="USA")

Why Use Functions?

Benefits of functions:

  • Organize code into meaningful blocks
  • Improve readability
  • Allow code reusability
  • Reduce errors
  • Make testing easier

In real projects, functions form the core structure of every script or application.




📝 Practice Exercises


  1. Create a function that returns the square of a number.
  2. Create a function that checks whether a number is even or odd.
  3. Create a function that accepts a name and prints a welcome message.
  4. Create a function that returns the largest of three numbers.
  5. Create a function that calculates the factorial of a number.

✅ Practice Answers


Answer 1:

def square(n):
    return n * n

print(square(5))

Answer 2:

def check(num):
    if num % 2 == 0:
        return "Even"
    else:
        return "Odd"

print(check(11))

Answer 3:

def welcome(name):
    print("Welcome,", name)

welcome("Sree")

Answer 4:

def largest(a, b, c):
    return max(a, b, c)

print(largest(10, 22, 15))

Answer 5:

def factorial(n):
    fact = 1
    for i in range(1, n + 1):
        fact *= i
    return fact

print(factorial(5))