SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Simple Functions
Exercise:
Algorithm Workbench
Question:4 | ISBN:9780132576376 | Edition: 2

Question

What will the following program display?
def main():
      x = 1
      y = 3.4
      print(x, y)
      change_us(x, y)
      print(x, y)
def change_us(a, b):
      a = 0
      b = 0
      print(a, b)
main()

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

The following output is generated when the given code is executed:

1 3.4
0 0
1 3.4

 

The line

1 3.4

will display  when the statement print(x,y) is executed

The variables x and y are initialised with values of 1 and 3.4, respectively, in the main() function. Printing the initial values is 1 3.4.

 

The line

0 0

will display

With the inputs x and y, the change_us() method is called. It's crucial to remember that the function's arguments a and b are independent variables that initially only get the values of x and y.

The variable X and Y in the main() function are unaffected by changes to a and b within the function.

The values 0 are given to variables a and b inside the change_us() function. The function prints the modified values as follows: 0 0.

 

The line

1 3.4

will display after the change_us() function call, the program returns to the main() function. The values of x and y remain unchanged since the modifications made in change_us() did not affect them.

Thus, the original values of x and y are printed again: 1 3.4.

0 0

Discussions

Post the discussion to improve the above solution.