The function of the muscle is to create tension, which is called contraction. Usually this contraction creates movement or maintains posture. One theory on how this works is that the sliding of the actin between the myosin filaments draws the walls of the sarcomere closer together. If the actin filaments get too close to each other they may overlap. Maximum contraction is reached when the myosin filaments hit the walls. When maximal compression happens the sarcomere may be shorter by twenty to fifty percent. This can vary depending upon the type of muscle and where it is in the body. Contraction requires the consumption of energy  in the muscle’s cells, but now we are getting too technical. 
When a force from outside the muscle is applied that moves the walls of the sarcomere apart, stretching occurs. The muscle cannot stretch itself. Studies  have shown that a muscle can normally stretch up to fifty percent or in some cases sixty-seven percent longer than its resting length, before rupturing occurs.
Again, in the sliding theory, stretching occurs when the filaments simply slide along each other, until the titin filaments can’t be extended any farther. Neither the actin nor the myosin filaments (in this theory) change lengths, but the titin filaments can and do lengthen. The theory postulates that the titin fibers are folded up like an accordion, with many V’s adjoining each other. Once pulled to its maximum length, the V’s get shallow and become straight. Once they are straight, the maximum length of the sarcomere is reached. 
The lengths of the resting sarcomeres are generally constant. A prominent theory holds that increased strength and range of motion in a muscle is caused by a large increase in the number of sarcomeres along the length of the fibers. Several studies have shown that the adding (or subtracting) of sarcomeres is not regulated by neuronal control (i.e., this is not directed by the brain), but by responses within the muscle itself.
- — This is provided by the conversion of adenosine triphosphate (ATP) into adenosine diphosphate (ADP) and phosphate (P).
- — For more information on this topic you may want to read The Science of Flexibility by Michael Alter.
- — Again, see Alter’s book.
- — At the scale of the whole body, when we stretch a muscle we want to feel the stretch in the middle of the muscle not at the attachment. This is true for the full muscle (do not stretch so much that you feel strain in the tendon, which is where the muscle attaches to the bone) as well as within the individual sarcomeres. During the first stage of stretching, the myosin and actin filaments move apart. Eventually the titin starts to unfold. Since the titin anchors the myosin filaments to the wall, too much stretching here can cause damage and tear the filaments.