In games NPC's do all kind of stuff.
Usually they are controlled by an advanced AI if they fight against or with you. That is nothing that can be programmed in a single function or small example.
If you mean npcs that stand around and that you can talk to and that give you quests:
their code is more simple, but it needs to be tailored to a dialog or quest system in your game, so I'm afraid it will be hard to give a generic example.
Such systems are complex, so don't expect that someone will post such a system here.
please be more precise what you want the NPC to do.
your best bet will be to look for a lite-c RPG template system. I don't know one, but it might exist.