183k views
8 votes
Write a program that will read two floating point numbers (the first read into a variable called first and the second read into a variable called second) and then calls the function swap with the actual parameters first and second. The swap function having formal parameters number1 and number2 should swap the value of the two variables

User Peuge
by
4.5k points

1 Answer

9 votes

Answer:

In Python:

def swap(number1,number2):

a = number1

number1 = number2

number2 = a

return number1, number2

first = float(input("First Number: "))

second = float(input("Second Number: "))

print("After Swap: "+str(swap(first,second)))

Step-by-step explanation:

The swap function begins here

def swap(number1,number2):

This saves number1 into variable a

a = number1

This saves number2 into number1

number1 = number2

This saves a (i.e. the previous number1) to number2

number2 = a

This returns the numbers (after swap)

return number1, number2

The main begins here

The next two lines prompt the user for first and second numbers

first = float(input("First Number: "))

second = float(input("Second Number: "))

This calls the swap function and print their values after swap

print("After Swap: "+str(swap(first,second)))

User Gomes
by
4.6k points