How to change numpy arrays in a function and return the values?

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:

In future posts, you can format code as formatted text by surrounding it with triple-backticks (```).