Iterate through ID’s in loop

You can access the current post ID within the loop via get_the_ID()

while ( $secondary_loop->have_posts() ) : $secondary_loop->the_post();
    rating( get_the_ID() );
endwhile;
wp_reset_postdata();