B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP User Defined Functions in Python | LiveCodeProgramming
Python Installation First Python Program Control Flow if...else nested if else match-case logical-operators for loops while loops break / continue Functions Defining Functions Function Arguments Function return value Lambda Function Recursion Exception Handling Exception Handling Try Except Else and Finally Raise Exception Custom Exceptions File Handling File in python Read File Write File Append File Delete File Exception Handling

User Defined Functions in Python

A user-defined function in Python is a block of reusable code created using the def keyword. Functions help organize code, reduce repetition, and improve readability.

Python में user-defined function एक ऐसा block होता है जिसे हम खुद def की मदद से define करते हैं। ये कोड को दुबारा इस्तेमाल करने में मदद करता है और प्रोग्राम को organized बनाता है।

Advantages of User Defined Functions
  • Code Reusability
  • Better Code Organization
  • Easy to Debug and Maintain
  • Improves Readability
Types of User Defined Functions
  • Without Parameters and Without Return Value
  • With Parameters and Without Return Value
  • Without Parameters and With Return Value
  • With Parameters and With Return Value
Examples
  1. Function with no parameter, no return value
    def greet():
        print("Hello, welcome!")
    
    greet()
    Output:
    Hello, welcome!
  2. Print current year
    def show_year():
        print("Year: 2025")
    
    show_year()
    Output:
    Year: 2025
  3. Function with parameter, no return
    def welcome(name):
        print("Hello", name)
    
    welcome("Abhishek")
    Output:
    Hello Abhishek
  4. Add two numbers
    def add(a, b):
        print("Sum:", a + b)
    
    add(10, 5)
    Output:
    Sum: 15
  5. No parameter, with return value
    def give_pi():
        return 3.14159
    
    print("Pi:", give_pi())
    Output:
    Pi: 3.14159
  6. Return fixed name
    def get_name():
        return "Python"
    
    print(get_name())
    Output:
    Python
  7. Parameter and return value
    def square(x):
        return x * x
    
    print("Square:", square(6))
    Output:
    Square: 36
  8. Max of two numbers
    def maximum(a, b):
        return a if a > b else b
    
    print("Max:", maximum(10, 20))
    Output:
    Max: 20
  9. Check even or odd
    def check_even(n):
        return "Even" if n % 2 == 0 else "Odd"
    
    print(check_even(7))
    Output:
    Odd
  10. Factorial using function
    def factorial(n):
        f = 1
        for i in range(1, n+1):
            f *= i
        return f
    
    print("Factorial:", factorial(5))
    Output:
    Factorial: 120
  11. Sum of list
    def list_sum(lst):
        return sum(lst)
    
    print(list_sum([1,2,3]))
    Output:
    6
  12. Print stars
    def print_stars():
        for i in range(5):
            print("*")
    
    print_stars()
    Output:
    *
    *
    *
    *
    *
  13. Table of number
    def table(n):
        for i in range(1, 11):
            print(n, "x", i, "=", n*i)
    
    table(4)
    Output:
    4 x 1 = 4
    ...
    4 x 10 = 40
  14. Check prime
    def is_prime(n):
        if n < 2:
            return False
        for i in range(2, n):
            if n % i == 0:
                return False
        return True
    
    print(is_prime(7))
    Output:
    True
  15. Global and local variable
    x = 50
    
    def show():
        x = 10
        print("Local x:", x)
    
    show()
    print("Global x:", x)
    Output:
    Local x: 10
    Global x: 50
  16. Nested function
    def outer():
        def inner():
            print("Inside inner")
        print("Inside outer")
        inner()
    
    outer()
    Output:
    Inside outer
    Inside inner