400,140 views
1 vote
1 vote
I have a variable and set it equal to 5. 1 then send it as an argument to a function that adds 5 to the variable passed in. Outside the function print the variable (after the function executes).

What will it print?
A. 5+1
B. 4
C. 6
D. 5

User Dan Johnson
by
2.2k points

1 Answer

17 votes
17 votes

Answer:

D. 5

Step-by-step explanation:

If the variables original value was 5 then the value that will be printed is 5. This is because the variable is being passed as a parameter into the function and 5 is being added to the parameter value, not the actual global variable outside. Therefore, in order for the adjusted value to be printed then that new value that is returned by the function needs to be saved in a new variable. This can be seen in the example code below.

I have a variable and set it equal to 5. 1 then send it as an argument to a function-example-1
User Zenaan
by
2.8k points