In the second case, the assignment c=np.dot(a,b) rebinds the local variable c to reference a different array. It doesn’t modify the original array, which is why the array referenced by f is unchanged after the function returns.
To modify the array referenced by c, you need to assign to some index instead of to the name c itself. For example, using a broadcasted assignment:
c[:, :] = np.dot(a, b)
or using NumPy’s ... shorthand:
c[...] = np.dot(a, b)
You can also use the matrix multiplication operator @ in place of np.dot:
c[...] = a @ b
For more on how assignments work in Python, I usually recommend Facts and myths about Python names and values | Ned Batchelder. You may also find these SO questions helpful:
- python - How do I pass a variable by reference? - Stack Overflow
- python - Why can a function modify some arguments as perceived by the caller, but not others? - Stack Overflow
- Are Python variables pointers? Or else, what are they? - Stack Overflow
In future posts, you can format code as formatted text by surrounding it with triple-backticks (```).