Skip to content
Programs · Program 7-5

Q.Program 7-5: Write a program using user defined function that accepts an integer and increments the value by 5. Also display the id of argument (before function call), id of parameter before increment and after increment.

Uttar Pradesh UpmspTextbookSubjective· 3mImportance★★★★★est
13% · 5/40 Questions
🔒 Locked · start free trial →

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-5 uses id() to reveal Python's parameter-passing: the parameter initially refers to the same object as the argument (same id), but incrementing an immutable int rebinds the parameter to a new object (new id), leaving the caller's variable unchanged.

The idea — pass by object reference. Python passes a reference to the object, not a copy. id() returns an object's identity (its memory address in CPython), so printing ids before and after the increment shows exactly when the parameter stops sharing the argument's object.

# Program 7-5: increment a value by 5 and observe the ids

def incrValue(num):
    print("id of parameter before increment:", id(num))
    num = num + 5                     # rebinds num to a NEW int object
    print("id of parameter after increment :", id(num))
    print("num after increment inside function:", num)

value = 10
print("id of argument before function call:", id(value))
incrValue(value)
print("value after the function call:", value)

Expected output — sample run (ids are memory addresses; the exact numbers will differ on your machine, but the pattern will not):

id of argument before function call: 9793280
id of parameter before increment: 9793280
id of parameter after increment : 9793440
num after increment inside function: 15
value after the function call: 10

Reading the run

  1. id(value) and the parameter's id before the increment are identical — argument and parameter are two names for the one object 10. …

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.