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
- Code after
- Can optionally be followed by expression or value, which becomes function's return value
- If no expression provided or
return
omitted, function returnsNone
by default (void function)
- If no expression provided or
- 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)
is25
, which is then added to10
in expression
- Return value of
- 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 variablename
for further usage (printing, concatenation)
- Return value of
Multiple returns in functions
- Functions can have multiple
return
statements to handle different scenarios or conditionsdef is_even(num): if num % 2 == 0: return True else: return False
- If
num
is divisible by 2 (even), function returnsTrue
- Otherwise, it returns
False
(odd numbers)
- If
- Multiple
return
statements can provide different return values based on specific conditionsdef get_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" else: return "F"
- Function returns different grade letters based on provided
score
value (argument) - First
return
statement that satisfies condition is executed - Function terminates immediately after executing matching
return
statement
- Function returns different grade letters based on provided
- 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