171k views
1 vote
Here, we investigate what happens when we change the value of a variable in a function. Your instructions are to

define a function that takes a single argument, adds 10 to that argument, and then prints it out (all inside the function!)
then, outside of the function, print out x, call your function using x as its argument, and print out x again
In total, x should be printed out 3 times. Did you get what you expected?
Lab: Foreshadowing References
1 # define your function here Nm
2
3
4 x = 10 non
5
6 # print out the value of x
7
8 # call your function on x
9
10 # print out the value of x

User Elkefreed
by
4.2k points

1 Answer

0 votes

Answer:

Observation:

The value of x remains the same in the main function

The value of x is incremented by 10 in the defined function

The program is as follows:

def Nm(x):

x += 10

print(x)

x = int(input("x: "))

print(x)

Nm(x)

print(x)

Step-by-step explanation:

The explanation is as follows:

This defines the function

def Nm(x):

This increments x by 10

x += 10

This prints x (the first print statement)

print(x)

The main begins here; This gets input for x

x = int(input("x: "))

This prints x (the second print statement)

print(x)

This calls the function

Nm(x)

This prints x (the third print statement)

print(x)

Assume that x = 5;

The first print statement (in the function) prints 15 i.e. 5 + 10 = 15

The second and third print statements (in the main function) prints 5, the exact value of x

User JimClarke
by
4.1k points