In this paper, a subroutine mechanism is introduced in genetic network programming for automatic program generation (GNP-APG). The proposed method named GNP with subroutines for APG (GNPsr-APG) is an extension of the algorithm of GNP-APG, where hierarchy programs are generated: in other words, programs that contain a main function and subroutines are generated. The proposed method automatically defines the main function and use of the potentially useful subroutines during evolution. By using subroutines, a complex program can be decomposed to several simple programs which are obtained more easily. Moreover, these subroutines are called many times from a main program, which results in reducing the size of the program significantly. The simulation results verify that the proposed method can improve the performance of GNP-APG and reduce the size of the program. © 2011 Institute of Electrical Engineers of Japan. Published by John Wiley & Sons, Inc.