This question requires an understanding that lists are objects. But I do not recall any discussion/slides/activities regarding how object references work.
In the question, a global variable, numbers, is defined as a list of numbers, and then passed as argument in a function call. That list is received by the parameter, numList, in the function definition. Changes to numList are made inside the function. There is no return statement.
After the function call is a console.log(numbers). The question asks what is the result of the console.log. The correct answer reflects the changes made to numList inside the function.
However, I have not covered this with the students. In fact, I have stressed that parameters are local variables to be used inside the function and, if you need the result of a function’s operations (excluding use of global variables with the same name) outside of the function, you need to include a return statement.
Am I misunderstanding the given code and/or where did we explain object references to students? Since it is a question on the Assessment I feel like I’ve just overlooked it somewhere.
Please advise - thank you!
@williamsj1 Thank you for the question! I have recategorized this thread as “Assessment” so that only verified teachers can see it.
As for the question itself, I am not going to copy it here because even though this is a locked forum, I know students are creative. The exam itself can be found here after logging in with a verified Teacher Account. I’m going to quote the answer key to begin my response:
Lesson 2 - Parameters and Return Investigate had students trace through functions using parameters and determine what different calls to those functions would result in.
Indeed, the students spend some time in Lessons 2 & 3 playing with parameters and seeing how changing them affects the output of a function.
Yes, you typically need a return statement to use the result of these operations within another area of the code. However, in the exam question, the function changeNums() simply updates the values within an array and doesn’t do anything with them. Therefore, we need to print the array to the console log in the last line of the code to see the updated values, which are the correct answer to the question.
Short version of the answer: the question is centered on asking the students to read the code and understand the logic of the math, moreso than it is on local vs. global variables.
Hope that helps,
Thank you, yes but I believe it updates the array inside the functions because a reference to the array is passed to the function. A reference is passed because an array/list is an object, not a primitive.
I do not believe this has been taught so far, am I wrong? Instead, we have explained parameters as primitive, like the following -
Following the code given in the assessment question, if you, instead,
- initialize a variable to hold a primitive value, say var numbers = 4;
- call the function with that variable changeNums(numbers, 3, 2)
- replace the for loop in the given changeNums function to just numList = 5
- the next line after the call is console.log(numbers), will print 4
Please advise - thanks
The way the question is written, it’s updating the values at each index of the list, and then storing them. The next line to print to the console log prints the updated values.
In the interest of transparency: my background is in a different branch of science, not CS. I’ve been facilitating CS Principles for a number of years, however. The question reads fairly straightforward to me, and I have never had another teacher raise concerns with this specific test item. I’ve flagged it for someone else to follow up, but in my experience a student with a good handle on the content should be able to understand what the question is asking.
Thank you, I appreciate your reply.
A good test to see the difference between an object and a primitive is to copy/paste the code segment into an area where you can run the code.
Then, change the code to set numbers = 5 instead of an array. Inside the function set the local variable, numList = 4. Run the code. You will see it will print numbers as 5, not 4.