wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

Write a program that has a user defined function to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of number1 and number 1 is reformed in place of number 2, otherwise the same order is returned.

Open in App
Solution

As per the question, the user-defined function should accept two numbers and then compare them before returning the values. Therefore, ‘if’ statement can be used inside the user-defined function to compare and return the values.

Program:
#defining a function to swap the numbers
def swapN(a, b):
if(a < b):
return b,a
else:
return a,b

#asking the user to provide two numbers
n1 = int(input("Enter Number 1: "))
n2 = int(input("Enter Number 2: "))

print("Returned value from function:")
#calling the function to get the returned value
n1, n2 = swapN(n1, n2)
print("Number 1:",n1," Number 2: ",n2)


OUTPUT:
Enter Number 1: 3
Enter Number 2: 4
Returned value from function:
Number 1: 4 Number 2: 3

flag
Suggest Corrections
thumbs-up
1
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Why Friction?-tackle
PHYSICS
Watch in App
Join BYJU'S Learning Program
CrossIcon