Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python function returns value

A function in Python can return a value to the caller using the return keyword. The returned value can be used by the caller as needed. In this tutorial, we will learn how to define functions that return values and how to use the returned values.

  • Defining a function that returns a value:

To define a function that returns a value, use the return keyword followed by the value or expression you want to return.

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

In this example, the add function takes two arguments, a and b, and returns their sum.

  • Calling a function that returns a value:

To call a function that returns a value, simply use the function name followed by the required arguments in parentheses. You can then store the returned value in a variable or use it in an expression.

result = add(5, 7)
print(result)  # Output: 12

In this example, we call the add function with the arguments 5 and 7, and the returned value is stored in the result variable.

  • Using a returned value in an expression:

You can directly use the returned value from a function in an expression without storing it in a variable.

print(add(3, 4) * 2)  # Output: 14

In this example, we call the add function with the arguments 3 and 4, and the returned value is directly used in the expression add(3, 4) * 2.

  • Returning multiple values:

In Python, you can return multiple values from a function by separating them with commas.

def divide_and_remainder(a, b):
    quotient = a // b
    remainder = a % b
    return quotient, remainder

When you call this function, the returned values will be packed into a tuple.

result = divide_and_remainder(11, 3)
print(result)  # Output: (3, 2)

You can also use tuple unpacking to directly assign the returned values to separate variables.

quotient, remainder = divide_and_remainder(11, 3)
print(quotient)   # Output: 3
print(remainder)  # Output: 2

In this tutorial, we covered how to define and call functions that return values, how to use returned values in expressions, and how to return multiple values. These concepts are essential for creating reusable and modular code in Python.

  1. How to return values from functions in Python:

    • Description: In Python, functions can return values using the return keyword.
    • Code:
      def add_numbers(a, b):
          return a + b
      
      result = add_numbers(3, 5)
      print(result)  # Output: 8
      
  2. Multiple return values in Python functions:

    • Description: Functions in Python can return multiple values using tuples or other data structures.
    • Code:
      def get_coordinates():
          return 3, 7
      
      x, y = get_coordinates()
      print(x, y)  # Output: 3 7
      
  3. Returning None in Python functions:

    • Description: If a function doesn't have a return statement, or it explicitly returns None, it is assumed to return None.
    • Code:
      def do_nothing():
          pass  # Implicitly returns None
      
      result = do_nothing()
      print(result)  # Output: None
      
  4. Handling different data types in function return values:

    • Description: Functions can return different data types based on the logic within the function.
    • Code:
      def get_result(value):
          if value > 0:
              return "Positive"
          elif value < 0:
              return "Negative"
          else:
              return "Zero"
      
  5. Type hints for function return values in Python:

    • Description: Type hints can be used to indicate the expected data type of the return value.
    • Code:
      def add_numbers(a: int, b: int) -> int:
          return a + b
      
  6. Returning tuples, lists, and dictionaries from functions in Python:

    • Description: Functions can return various data structures like tuples, lists, or dictionaries.
    • Code:
      def get_data():
          return (1, 2, 3), [4, 5, 6], {'a': 7, 'b': 8}
      
  7. Returning custom objects in Python functions:

    • Description: Functions can return instances of custom-defined classes.
    • Code:
      class Person:
          def __init__(self, name, age):
              self.name = name
              self.age = age
      
      def create_person():
          return Person("John", 25)
      
  8. Returning functions as values in Python:

    • Description: Functions can return other functions.
    • Code:
      def get_multiplier(factor):
          def multiplier(x):
              return x * factor
          return multiplier
      
      times_two = get_multiplier(2)
      result = times_two(5)
      print(result)  # Output: 10
      
  9. Function chaining and return values in Python:

    • Description: Functions can be chained by having each function return an object on which the next function is called.
    • Code:
      def add(x):
          return x + 5
      
      def multiply(x):
          return x * 2
      
      result = multiply(add(3))
      print(result)  # Output: 16
      
  10. Returning values from recursive functions in Python:

    • Description: Recursive functions can return values at each level of recursion.
    • Code:
      def factorial(n):
          if n == 0 or n == 1:
              return 1
          else:
              return n * factorial(n-1)