Programs · Program 7-12
Q.Program 7-12: Write a program using user defined function that accepts length and breadth of a rectangle and returns the area and perimeter of the rectangle.
Sikkim CbseNCERTSubjective· 3mImportance★★★★★est
35% · 14/40 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Program 7-12 returns two values from one function: return area, perimeter packs them into a tuple, and area, perimeter = rectangle(l, b) unpacks them in the caller.
The idea. A Python function can hand back any number of results in one return — the comma builds a tuple, and tuple unpacking on the caller's side gives each value its own name. That is cleaner than printing inside the function or calling twice.
# Program 7-12: area and perimeter of a rectangle from one function
def rectangle(length, breadth):
area = length * breadth
perimeter = 2 * (length + breadth)
return area, perimeter # a tuple (area, perimeter)
l = float(input("Enter length of the rectangle: "))
b = float(input("Enter breadth of the rectangle: "))
area, perimeter = rectangle(l, b)
print("Area :", area)
print("Perimeter :", perimeter)
Key lines
return area, perimeter— equivalent toreturn (area, perimeter); one object (a tuple) travels back.area, perimeter = rectangle(l, b)— unpacking; the order matches the order in thereturn.
Expected output — sample run with length 7.5 and breadth 4:
Enter length of the rectangle: 7.5 …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.