I would also like to add to this that in case of grave matter the person can be forgiven with just a prayer, in which they express sorrow for them. Such cases would probably include lets say you usually go to confession once a week and you unexpectedly die before then. That is why constant prayer and repentance is necessary, since we never know the moment of our death. Also, in other grave cases you must do an act of contrition and promise to confess your sins to a priest as soon as possible, in such a case you can recieve communion. They say that if in 30 days you absolutely do not have the opportunity for confession, this is valid to do. Also, in life threatening cases too. However, we always need to go to confession even if we pray our hearts out prior to it. Confession can also cleanse the stain of sin, thus eliminating temporal punishment in purgatory.
May God bless you.