You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

6.5 Return values

2 min readjune 24, 2024

Functions in Python are powerful tools that let you organize and reuse code. They can take input, process it, and give back results using statements. This makes your code more efficient and easier to understand.

are the output of functions, which you can use in other parts of your code. By using multiple return statements, you can handle different scenarios within a single , making your code more flexible and responsive to various conditions.

Functions and Return Values

Function of return statements

Top images from around the web for Function of return statements
Top images from around the web for Function of return statements
  • Immediately terminates function execution and returns control back to calling code
    • Code after
      return
      statement within function will not be executed
  • Can optionally be followed by expression or value, which becomes function's return value
    • If no expression provided or
      return
      omitted, function returns
      [None](https://www.fiveableKeyTerm:None)
      by default ()
  • Returned value can be captured by calling code and used for further processing or assignment
  • Multiple
    return
    statements can be used within function to provide different return values based on certain conditions
    • Allows for of function based on specific scenarios (error handling, input validation)

Usage of return values

  • Value returned by function can be directly used in expressions or assignments
    [def](https://www.fiveableKeyTerm:def) square(x):
        return x ** 2
    
    result = square(5) + 10
    print(result)  # Output: 35
    
    • Return value of
      square(5)
      is
      25
      , which is then added to
      10
      in expression
  • Return values can be assigned to variables for later use
    def get_name():
        return "John"
    
    name = get_name()
    print("Hello, " + name)  # Output: Hello, John
    
    • Return value of
      get_name()
      is assigned to variable
      name
      for further usage (printing, concatenation)

Multiple returns in functions

  • Functions can have multiple
    return
    statements to handle different scenarios or conditions
    def is_even(num):
        if num % 2 == 0:
            return True
        else:
            return False
    
    • If
      num
      is divisible by 2 (even), function returns
      True
    • Otherwise, it returns
      False
      (odd numbers)
  • Multiple
    return
    statements can provide different return values based on specific conditions
    def get_grade(score):
        if score >= 90:
            return "A"
        elif score >= 80:
            return "B"
        elif score >= 70:
            return "C"
        else:
            return "F"
    
    1. Function returns different grade letters based on provided
      score
      value ()
    2. First
      return
      statement that satisfies condition is executed
    3. Function terminates immediately after executing matching
      return
      statement
  • Useful for implementing complex logic or branching paths within function (data validation, error handling, game logic)

Function Design and Effects

  • Parameters are variables defined in the function's declaration that accept input values
  • Functions can have side effects, which are changes made to the program's state outside the function's local
  • Pure functions always produce the same output for the same input and have no side effects
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary