One of the ways a religion survives is by separating itself and costing something to be part of it. The Hebrews did this with dietary laws, for example. It is somewhat paradoxical, but it seems to hold up. Of course, if people don't like the cost they leave. Whatever rules you set up are going to lose you some people, but in the long run, having a core of people who have paid some cost and continue to pay it is what works. Religions that don't cost have friends but few adherents.
I suppose this applies for any type of group, not just a religion. Military groups, political groups...
An additional strategy is to make it even more expensive to leave. You are going to hell if you leave. You are dead to us if you marry outside the group. In the case of Islamic apostasy, that can be literally true. Not everyone does this - the Amish don't. However the separation from the old group is usually quite real.
I think of this in terms of evangelical children of the last generation, the only one I really know about. Separation was supposed to include certain rock bands that should not be listened to, and popular entertainments in general were highly suspect. It was probably not the best place to make a stand, and the way that it was applied - allowing country music with other bad messages, not listening carefully to what was actually being condemned, focusing on beats and bass guitars and volume - could get ridiculous. Still, art is a powerful persuader, and recognising that wasn't crazy. It has also turned out to have some truth in it: those who identify the ridiculousness of not being allowed to watch what they want and listen to what they want seem to have been greatly influenced by the values of that art.
Which is cart and which is horse I don't know. Those who left because they didn't like paying that cost might have left whatever cost was being asked. They just didn't want to stay, laughing at rules against Black Sabbath was a reason that was handy. Parents, teachers, or other kids being jerks are also identified as reasons to leave. A Christian school had a different winter vacation week one year and offered as its reason "Be ye not conformed to the world." There is usually something to that jerkness accusation - except that it is also true of everybody else. If they want to go, they'll find something.
If we grant that to bring up children in the faith it must cost them something, what is the right thing to select? Whatever we choose, some children will find the cost too high - or too ridiculous - and will leave. We want to set it up that we don't give children unnecessary extra reasons to leave.