All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad, is His slave and Messenger.
If you gave up attending mixed gatherings because you know that it is prohibited, this necessitates holding the intention of earning the Pleasure of Allah, The Exalted, by complying with His Prohibitions, and you will be rewarded for avoiding what is prohibited.
You will also be rewarded for fasting because you know that it is recommended; you have essentially intended to earn the Pleasure of Allah, The Exalted, by obeying Him. By doing so, you have successfully held the intention of avoiding His Prohibitions by giving up the prohibited act in the first case and performing the act of obedience in the second case. Imam Ash-Shaatibi said in Al-Muwaafaqaat: “The correct opinion is that the intention is a condition for considering a deed to be an act of worship, and the intention that is meant here is the intention to comply with the Commands of Allah and avoid His Prohibitions.” [End of quote]
Allah Knows best.