Algorithm
Function Definition Algorithm:
- Start
- Define a function with a meaningful name
- Specify input parameters, if any
- Declare variables for multiple return values
- Perform computations to determine return values
- Return the values using the
return
statement - End
-
def return_multiple_values(input_parameter1, input_parameter2, ...): result1 = ... result2 = ... # Perform computations # ... return result1, result2, ...
-
Function Call Algorithm:
- Start
- Call the function with appropriate arguments
- Capture the returned values in variables
- End
-
output1, output2, ... = return_multiple_values(input_value1, input_value2, ...)
-
Use Returned Values Algorithm:
- Start
- Utilize the captured values for further processing or display
- End
-
print("Result 1:", output1) print("Result 2:", output2) # ...
Note: This is a high-level algorithmic representation, and you can adapt it to the specific details and requirements of your progra
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
# Function Definition
def calculate_rectangle_properties(length, width):
# Calculate area and perimeter of a rectangle
area = length * width
perimeter = 2 * (length + width)
# Return multiple values
return area, perimeter
# Function Call
length_input = float(input("Enter the length of the rectangle: "))
width_input = float(input("Enter the width of the rectangle: "))
# Call the function and capture the returned values
rectangle_area, rectangle_perimeter = calculate_rectangle_properties(length_input, width_input)
# Use Returned Values
print("\nRectangle Properties:")
print("Area:", rectangle_area)
print("Perimeter:", rectangle_perimeter)
Copy The Code &
Try With Live Editor
Output
Enter the width of the rectangle: 3
Rectangle Properties:
Area: 15.0
Perimeter: 16.0
#2 Code Example- Return values using comma
Code -
Python Programming
def name():
return "John","Armin"
# print the tuple with the returned values
print(name())
# get the individual items
name_1, name_2 = name()
print(name_1, name_2)
Copy The Code &
Try With Live Editor
Output
John Armin
#3 Code Example- Using a dictionary
Code -
Python Programming
def name():
n1 = "John"
n2 = "Armin"
return {1:n1, 2:n2}
names = name()
print(names)
Copy The Code &
Try With Live Editor
Output
Demonstration
Python Programing Example to Return Multiple Values From a Function-DevsEnv