A measurement of the fragmentation functions of jets into charged particles in p+Pbcollisions and pp collisions is presented. The analysis utilizes 28 nb−1 of p+Pb data and 26 pb−1 of pp data, both at sNN= 5.02 TeV, collected in 2013 and 2015, respectively, with the ATLAS detector at the LHC. The measurement is reported in the centre-of-mass frame of the nucleon–nucleon system for jets in the rapidity range ⁎|y⁎|<1.6 and with transverse momentum 45 <pT< 260 GeV. Results are presented both as a function of the charged-particle transverse momentum and as a function of the longitudinal momentum fraction of the particle with respect to the jet. The pp fragmentation functions are compared with results from Monte Carlo event generators and two theoretical models. The ratios of the p+Pb to pp fragmentation functions are found to be consistent with unity.