Fiveable

๐ŸIntro to Python Programming Unit 6 Review

QR code for Intro to Python Programming practice questions

6.5 Return values

๐ŸIntro to Python Programming
Unit 6 Review

6.5 Return values

Written by the Fiveable Content Team โ€ข Last updated September 2025
Written by the Fiveable Content Team โ€ข Last updated September 2025
๐ŸIntro to Python Programming
Unit & Topic Study Guides

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

Return values 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 function, making your code more flexible and responsive to various conditions.

Functions and Return Values

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 by default (void function)
  • 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 early termination 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 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 (argument)
    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 scope
  • Pure functions always produce the same output for the same input and have no side effects