Advertisements
Advertisements
Question
Write the overloaded function definitions of add()- on adds two numbers and other concatenates two strings.
Code Writing
Advertisements
Solution
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
Is there an error in this question or solution?
