B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Function Returning Value | User-Defined Function Examples
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

Python Function with Return Value

When a function returns a value, it gives back a result to the caller using the return statement. This is useful when you want to use the result later or assign it to a variable.

जब कोई function value return करता है, तो वह return statement का उपयोग करके result वापस देता है। यह तब उपयोगी होता है जब आप उस result को आगे use करना चाहते हैं या किसी variable में store करना चाहते हैं।

10 Examples of Functions Returning Values
  1. Return square of a number
    def square(n):
        return n * n
    
    print("Square:", square(4))
    Output:
    Square: 16
  2. Return sum of two numbers
    def add(a, b):
        return a + b
    
    result = add(3, 7)
    print("Sum:", result)
    Output:
    Sum: 10
  3. Return maximum of two
    def max_num(a, b):
        return a if a > b else b
    
    print("Max:", max_num(5, 10))
    Output:
    Max: 10
  4. Return full name
    def full_name(fname, lname):
        return fname + " " + lname
    
    print(full_name("John", "Doe"))
    Output:
    John Doe
  5. Return average of three numbers
    def average(a, b, c):
        return (a + b + c) / 3
    
    print("Average:", average(10, 20, 30))
    Output:
    Average: 20.0
  6. Check if number is even
    def is_even(n):
        return n % 2 == 0
    
    print("Is 4 even?", is_even(4))
    Output:
    Is 4 even? True
  7. Return list of squares
    def squares(n):
        return [i*i for i in range(1, n+1)]
    
    print(squares(5))
    Output:
    [1, 4, 9, 16, 25]
  8. Return factorial
    def factorial(n):
        if n == 0:
            return 1
        return n * factorial(n - 1)
    
    print("Factorial:", factorial(5))
    Output:
    Factorial: 120
  9. Check palindrome
    def is_palindrome(s):
        return s == s[::-1]
    
    print("Radar is palindrome:", is_palindrome("radar"))
    Output:
    Radar is palindrome: True
  10. Return grade based on marks
    def get_grade(marks):
        if marks >= 90:
            return "A"
        elif marks >= 75:
            return "B"
        elif marks >= 60:
            return "C"
        else:
            return "F"
    
    print("Grade:", get_grade(82))
    Output:
    Grade: B