The function must receive arguments and return some data. Why does this function work in fact as a procedure, changing the arguments that were filed? Thank you
def cofactor (array, i, q): # algebraic addition ((-1) ^ (i + j) * Mi, j) For k in Range (i): Array [K] .pop (Q) Array.pop (I) Return Array. #Return (-1) ** (i + Q) * Determinant (Array) array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] For i in Range (3): For Q In Range (3): Print ('Array:', Array) Print (COFACTOR (Array, I, Q)) `` `
Answer 1, Authority 100%
In Python, there is no concept of procedure, only functions and methods (these are functions in classes).
and objects are transmitted by reference.
In addition, the
array list is changeable, so if you do not want to change it inside the function and return the modified copy, then make a copy of the list via Copy.DeepCopy :
import copy Def Cofactor (Array, I, Q): # Algebraic supplement ((-1) ^ (i + j) * mi, j) Array = Copy.DeepCopy (Array) For k in Range (I): Array [K] .pop (Q) Array.pop (I) Return Array.
In Python, there is no difference between the function and the procedure. In Python, even if you do not return anything using
Return explicitly, the value of
Next. In other languages, the same story with the reference types passed to the procedure / function you see in this code: You cannot change the pointer itself to the transmitted object, but you can easily change its contents.
Yes, this is not a very good style when the procedure / function changes something transmitted to her, which you do not expect, but there is no insurance from such behavior.