The essence of Ps 40 is that it is a part of a liturgy, and the formal and substantial changes within the psalm are to be understood against the background of progression within the liturgy. The liturgy begins with thanksgiving, thereby establishing precedent and laying a foundation for what is to follow. It then moves on to lament and prayer; it is only in the prayer that the overall purpose of the liturgy emerges, and the preparatory role of the thanksgiving is clarified.
PETER C. CRAIGIE AND MARVIN TATE, PSALMS 1-50, VOLUME 19: SECOND EDITION (WORD BIBLICAL COMMENTARY), 314.