I am confused because I don’t understand why do we need to pass array as argument here, when it works same if we only pass n, which makes it simpler for me to understand.
The main reason is such function would depend on the arr variable from the outer scope. This makes it less usable, and requires additional care to be able to use it with various arrays.
When array is passed as one of the arguments, function doesn’t care what is the variable name of the array outside of the function. It has everything it needs passed in.