Include external code in function

There are hundreds of examples all over this forum.

There is a built in importable demo too
CTRL-I