in reply to use strict and subroutines

The first version doesn't work, because you declare $myname inside the subroutine. With the end of the subroutine $myname goes out of scope and is not defined in your main program.

The second version works, because you declare $myname globally, so it goes out of scope at the end of your program.

You don't have to use return. If you omit it, the last value created in the sub will be returned, in your case the value of $myname.