हिंदी

Write the overloaded function definitions of add()- on adds two numbers and other concatenates two strings.

Advertisements
Advertisements

प्रश्न

Write the overloaded function definitions of add()- on adds two numbers and other concatenates two strings.

कोड लेखन
Advertisements

उत्तर

Python cannot define two separate overloaded functions with the same name. However, the same add() function can perform both operations because + is polymorphic.

def add(a, b):
    return a + b  # Adds numbers or concatenates strings

print(add(10, 20))
print(add("Hello ", "World"))

30
Hello World

Here, add() is a single function; it is not traditional function overloading in Python.

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 2: Concept of Object Oriented Programming - EXCERCISE [पृष्ठ ३७]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 2 Concept of Object Oriented Programming
EXCERCISE | Q 12. | पृष्ठ ३७
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×