Question

12 points). The function swap() should swap two integers and main() should print those two swapped integers. What (if anythin
on line 9, it is illegal syntax to introduce a new variable named temp. Instead, temp should be passed in as a parameter and
pick two answers
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Yes given Code is Wrong and these two options are the reason behind that :

Option 3) The function swap() should return the swapped values, so main() can access it.

Option 4) The function swap() does not currently swap the values a and b. This is a logical error

This code has a logical error

If we use
a = b, original value of a will be lost and during
temp = a, temp will also have the new value of 'a' i.e. a=b So you can't return multiple values in functions.

To make it happen, you have to create global variables that are accessible to both functions.

So Correct Code for this program will be

def main():
x=10
y=2
x, y=swap(x,y)
print(x,y)
  
def swap(a, b):
temp=a
a=b
b=temp
return (a,b)
main()

OUTPUT

- X Spyder (Python 3.7) File Edit Search Source Run Debug Consoles Projects Tools View Help D A 4 @ 21 CEE C:\Users\rshub\.sp

Add a comment
Know the answer?
Add Answer to:
pick two answers 12 points). The function swap() should swap two integers and main() should print...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose you wanted to write a function to swap the value of two integers. int x...

    Suppose you wanted to write a function to swap the value of two integers. int x = 10; int y = 20; // call the swap function on x and y // now x = 20, y = 10 Using C, write such a function.

  • #Which function has the most parameters? def Menu(): print ("Your Name Main Menu") print("1. Sum") print("2....

    #Which function has the most parameters? def Menu(): print ("Your Name Main Menu") print("1. Sum") print("2. Product") print("3. information") print("4. Quit") def Sum(x): x= x* 2 y = 10 answer = x+y return x,y,answer def Product(w,z): answer = w*z print (answer) def Calc(a,b,c): return a*b*c Question options: Product Calc Menu Sum

  • Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code....

    Lab 6.6 – Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference Parameters 2 // This program uses a function to swap the values in two variables . 3 // PUT YOUR NAME HERE. 4 #include <iostream> 5 using namespace std; 6 7 // Function prototype 8 void swapNums(int, int); 9 10 /***** main *****/ 11 int main() 12 { 13 int num1 = 5, 14...

  • This program should use a main function and two other functions named playlist and savelist as...

    This program should use a main function and two other functions named playlist and savelist as follows: The main function: The main function should create an empty list named nums and then use a loop to add ten integers to nums, each integer in the range from 10-90. NOTE: In Python, a list is a data type. See Chapter 7. The main function should then call the playlist function and savelist function, in that order. Both of these functions take...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Your task is to: 1. Introduce function documentation (the triple-quoted string that precedes the definition of...

    Your task is to: 1. Introduce function documentation (the triple-quoted string that precedes the definition of a Python function) 2. Make sundry changes to improve the readability of the code. This might include: 1. Be sure to change the function names to something appropriate. 2. If there are any "gotchas" or assumptions about the input, document those. 3. If the variables are poorly named, change the variable names. 4. If the logic is needlessly complicated or redundant, abbreviate it. 3....

  • Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and...

    Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and y and returns the unsigned 64 bit integer sum z, i.e. zx+y. In your main function, you should assume that x, y, and z will be stored in the following 6 registers as follows: x: upper 32 bits in $t1 y: upper 32 bits in St3 z: upper 32 bits in St5 lower 32 bits in $to lower 32 bits in $t2 lower 32...

  • I am supposed to a pseudocode function named max that accepts two integer values as arguments...

    I am supposed to a pseudocode function named max that accepts two integer values as arguments and returns the value that is greater of the two. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is greater of the two. Is everything correct and complete? //Pseudocode //This function takes two integers as parameters: x and y Begin : Max(x,y) If(x>y) then MAX=x Else MAX=y End if Return...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT