Skip to content
Programs · Program 7-3

Q.Program 7-3: Write a user defined function to add 2 numbers and display their sum.

CBSENCERTSubjective· 2mImportance★★★★★est
8% · 3/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-3 defines the simplest kind of user-defined function — no parameters, no return value: addnum() inputs two numbers, adds them, and displays the sum; the definition does nothing until the function is called.

The idea. A user-defined function is created with def, a name, parentheses and an indented body. This first example deliberately keeps everything inside the function — input, processing and output — to show the mechanics of definition vs call.

# Program 7-3: user defined function to add two numbers and display the sum

def addnum():
    fnum = int(input("Enter first number: "))
    snum = int(input("Enter second number: "))
    total = fnum + snum
    print("The sum of", fnum, "and", snum, "is", total)

# function call
addnum()

Key lines

  • def addnum(): — defines the function; Python just remembers the body, it does not run it.
  • addnum() — the call; only now does control jump into the body, execute the three statements, and return to the line after the call.
  • int(input(...)) — input() returns a string; int() converts it so + performs arithmetic, not string concatenation. …

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.