John Calvin has an excellent explanation on fasting in the Institutes of Christian Religion, Book IV, Chapter 12, Sections 14-21. Fasting is confusing and necessary at once. Fasting does not mean starving yourself in order to make God do something you want. Do a work and expecting God to compensate you for that work is Paganism. Here is an excerpt from the Institutes where Calvin explains that fasting is a means for focusing prayer and devotion, not an act of appeasement:
INSTITUTES IV.12.19 : Another evil akin to this, and greatly to be avoided, is, to regard fasting as a meritorious work and species of divine worship. For seeing it is a thing which is in itself indifferent, and has no importance except on account of those ends to which it ought to have respect, it is a most pernicious superstition to confound it with the works enjoined by God, and which are necessary in themselves without reference to any thing else. Such was anciently the dream of the Manichees, in refuting whom Augustine clearly shows that fasting is to be estimated entirely by those ends which I have mentioned, and cannot be approved by God, unless in so far as it refers to them.
God will not answer your prayer because you fasted a certain number of days, but fasts allow us to focus our prayers! Great wisdom that I wish I had heard said before.
What is the purpose of fasting? Calvin gives three ends:
INSTITUTES IV.12.15 : A holy and lawful fast has three ends in view. We use it either to mortify and subdue the flesh, that it may not wanton, or to prepare the better for prayer and holy meditation; or to give evidence of humbling ourselves before God, when we would confess our guilt before him.
The first end is not very often regarded in public fasting, because all have not the same bodily constitution, nor the same state of health, and hence it is more applicable to private fasting.
The second end is common to both, for this preparation for prayer is requisite for the whole Church, as well as for each individual member.
The same thing may be said of the third. For it sometimes happens that God smites a nation with war or pestilence, or some kind of calamity. In this common chastisement it behaves the whole people to plead guilty, and confess their guilt. Should the hand of the Lord strike any one in private, then the same thing is to be done by himself alone, or by his family. The thing, indeed, is properly a feeling of the mind. But when the mind is affected as it ought, it cannot but give vent to itself in external manifestation, especially when it tends to the common edification, that all, by openly confessing their sin, may render praise to the divine justice, and by their example mutually encourage each other.
Calvin considers why the Disciples didn't fast and when it is appropriate to fast and all the abuses of fasting. I highly recommend reading Institutes of Christian Religion, Book IV, Chapter 12, Sections 14-21. It's only about 5 pages.
Update: Some words on fasting by John Piper: The Crazy Idea of Fasting in 09.
Related: Fasting, Institutes, John Calvin