KNN with Function Loop