Skip to content
Advertisement

How to make recursive function hold it’s original parameter value?

I’m trying to write a function that calculates the determinant of a square matrix using recursion.

The parameter of the oldest function – the first one called – changes to the matrix returned by scale_down() and I don’t know why.

JavaScript

I tried substituting “matrix” with “var” in order not to use it directly, but it amounted to nothing.

The code would work fine, I think, if after every for loop iteration in the oldest la_place call, the parameter “matrix” would still equal the original 3×3 matrix. That was my original idea.

Advertisement

Answer

I figured it out. It turns out that var = matrix does not make var independent of matrix. I imported the “copy” library and used var = copy.deepcopy(matrix) to make var independent of matrix. Here is the solution:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement