Functions in Python

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

We can define our own functions using the def keyword. Once defined, the function can be called (used) multiple times.

Syntax:

def function_name():
    # block of code
    statements

# Calling the function
function_name()

Note: Function name should follow naming rules and it should be meaningful. The block of code inside the function is indented.

Types of Functions in Python

In Python, functions are mainly of two types:

  1. Built-in Functions (Predefined / Inbuilt Functions): These are already provided by Python. Examples include print(), len(), type(), sum() etc.
  2. User-defined Functions: These are the functions we create ourselves using the def keyword, to perform a specific task as per our need.

Example of Built-in Function:

numbers = [1, 2, 3, 4]
print("Length is:", len(numbers))

Output:

Length is: 4

Explanation:

  • len() is a built-in function that returns the number of items in a list.
  • We didn't have to define len ourselves. Python provides it by default.

Example of User-defined Function:

Example 1: Basic function without parameters

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

greet()

Output:

Hello, welcome to Python Functions with ShikshaSanchar!

Explanation:

  • We created a function named greet() using the def keyword.
  • The function prints a welcome message.
  • We called the function by writing greet() and it displayed the message.

Example 2: Function with parameters

def add_numbers(a, b):
    result = a + b
    print("Sum is:", result)

add_numbers(5, 7)

Output:

Sum is: 12

Explanation:

  • The function add_numbers() takes two parameters: a and b.
  • Inside the function, it calculates their sum and prints it.
  • We call the function with arguments 5 and 7, so output is Sum is: 12.

Click the next page to know more about Arguments and Return values in Python.

Parts of a Function (Table):

Part Description
def keyword Used to define a function.
Function name Name of the function (should follow variable naming rules).
Parameters Optional. Values that we can pass to the function.
Function body The block of code that runs when the function is called.
Return statement Optional. Used to return a value from the function.

Advantages of Functions

  • Reusability: Once a function is written, we can use it multiple times without rewriting the code.
  • Modularity: Large programs can be divided into small functions which makes code organized and manageable.
  • Easy Debugging: Errors can be easily found and fixed because we can test functions independently.
  • Improves Readability: Functions make the program easier to understand by breaking it into smaller parts.
  • Avoids Repetition: Reduces duplication of code which saves time and effort.

Summary:

  • A function is a reusable block of code.
  • Functions are defined using the def keyword.
  • Functions can have parameters (input values).
  • Functions make our code clean, short, and reusable.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar