Combine the sugar and flour (or cornstarch) in a medium saucepan. Stir in the milk. Cook and stir over medium heat until mixture is thickened and bubbly. Cook and stir for 2 minutes more.
Remove from heat. Gradually stir about 1 cup of the milk mixture into beaten egg yolks or eggs. Note: adding the hot milk mixture into the eggs too quickly will cause the eggs to cook rapidly into a scrambled egg.
Return all of the milk and egg mixture to the saucepan. If using only egg yolks, bring to a gentle boil. If using whole eggs, cook until nearly bubbly, but do not allow the mixture to boil. Reduce heat.
Reduce heat. Cook and stir for 2 minutes more.
Remove from heat. Stir in butter (or margarine) and vanilla.
Pour pudding into a bowl, and cover the surface with clear plastic wrap.
Chill, and do not stir.