Functions in Python Programming

Reading Time: 3 minutes

What is a Function?

A function is a block of code that performs a specific task and can be reused multiple times in a program.

Why Use Functions ?

Without functions, programmers would need to write the same code again and again, which makes programs longer and harder to manage.

Example without function:

print("Hello Aman")
print("Hello Rahul")
print("Hello Priya")

Using function:

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

greet("Aman")
greet("Rahul")
greet("Priya")

This makes the code shorter, cleaner, and reusable.

Defining Functions in Python

In Python, functions are defined using the def keyword.

Basic Syntax

def function_name():
    # code block

Example: Simple Function

def greet():
    print("Welcome to Python Programming")

greet()

Output:

Welcome to Python Programming

Key Points While Defining Functions

  • Use def keyword
  • Function name should be meaningful
  • Parentheses () are required
  • Colon : is used after function declaration
  • Proper indentation is mandatory

Function Naming Rules

  • Must start with a letter or underscore
  • Cannot use keywords like if, for, while
  • Should follow lowercase naming convention
  • Use descriptive names like calculate_sum()

Parameters and Arguments

Functions can take input values called parameters. These inputs help functions perform operations dynamically.

Function with Parameters

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

greet("Aman")

Output:

Hello Aman

Multiple Parameters

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

add(5, 3)

Types of Arguments

1. Positional Arguments

Values are passed in correct order.

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

student("Aman", 20)

2. Keyword Arguments

Arguments are passed using parameter names.

student(age=20, name="Aman")

3. Default Arguments

Default values are assigned if no value is provided.

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

greet()
greet("Rahul")

4. Variable-Length Arguments

Used when number of inputs is unknown.

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

add(1, 2, 3, 4)

Return Values in Functions

Functions can return values using the return keyword.

Example:

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

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

Output:

15

Why Use Return?

  • To send result back to the caller
  • To reuse output in other calculations
  • To make functions more flexible

Returning Multiple Values

def operations(a, b):
    return a + b, a - b

x, y = operations(10, 5)
print(x, y)

Built-in Functions

Built-in functions are pre-defined functions available in Python.

Examples:

print("Hello")
len("Python")
type(10)
max(5, 10, 20)

These functions are ready to use without defining them.

Common Built-in Functions

FunctionDescription
print()Displays output
len()Returns length
type()Returns data type
sum()Adds values
max()Finds maximum

User-Defined Functions

User-defined functions are created by programmers to perform specific tasks.

Example:

def square(x):
    return x * x
print(square(4))

Output:

16

Advantages

  • Custom functionality
  • Code reuse
  • Better readability
  • Easier debugging

Scope of Variables

Variables inside functions have limited scope.

Local Variable

def test():
    x = 10
    print(x)
test()

Global Variable

x = 5
def show():
    print(x)
show()

Lambda Functions (Anonymous Functions)

A lambda function is a small function without a name.

add = lambda a, b: a + b
print(add(5, 3))

Recursive Functions

A function that calls itself is called recursion.

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
print(factorial(5))

Practical Examples

Example 1: Even or Odd

def check_even(num):
    if num % 2 == 0:
        return "Even"
    return "Odd"
print(check_even(7))

Example 2: Calculator

def calculator(a, b):
    return a + b, a - b, a * b
print(calculator(10, 5))

Example 3: Find Maximum

def find_max(a, b):
    if a > b:
        return a
    return b
print(find_max(10, 20))

Stay Updated With The Latest Content

Follow The Channel