Measurements of fragmentation functions for jets associated with an isolated photon are presented for the first time in pp and PbPb collisions. The analysis uses data collected with the CMS detector at the CERN LHC at a nucleon-nucleon center-ofmass energy of 5.02 TeV. Fragmentation functions are obtained for jets with p jet T > 30 GeV/c in events containing an isolated photon with p γ T > 60 GeV/c, using charged tracks with transverse momentum p trk T > 1 GeV/c in a cone around the jet axis. The association with an isolated photon constrains the initial p T and azimuthal angle of the parton whose shower produced the jet. For central PbPb collisions, modifications of the jet fragmentation functions are observed when compared to those measured in pp collisions, while no significant differences are found in the 50% most peripheral collisions. Jets in central PbPb events show an excess (depletion) of low (high) p T particles, with a transition around 3 GeV/c. This measurement shows for the first time the in-medium shower modifications of partons (quark dominated) with well defined initial kinematics. It constitutes a new well-controlled reference for testing theoretical models of the parton passage through the QGP. the pseudorapidity coverage up to |η| = 5.2. In the case of PbPb events, the HF signals are used to determine the degree of overlap ("centrality") of the two colliding Pb nuclei [21] and the event-by-event azimuthal angle of maximum particle density ("event plane") [35]. A more detailed description of the CMS detector can be found in Ref. [36].The event samples are selected online with a dedicated photon trigger requiring one photon with p γ T > 40 GeV/c [29], and are subjected to offline requirements to remove non-collision events [29,37]. For jets and photons, the reconstruction algorithms, analysis selections, and corrections for the energy scale and resolution are the same as in Ref. [29]. For the analysis of PbPb collisions, the event centrality is defined as the fraction of the total inelastic hadronic cross section, starting at 0% for the most central collisions, and is evaluated as percentiles of the distribution of the energy deposited in the HF calorimeters [21]. Results are presented in four centrality intervals, ranging from a central 0-10% (i.e., the 10% of the events having the largest overlap area of the two nuclei [38]), to a peripheral 50-100% (the one closest to a pp-like environment) intervals.The photon candidates are restricted to the barrel region of the ECAL, |η γ | < 1.44, and are required to have p γ T > 60 GeV/c. Electron contamination and anomalous signals caused by the interaction of heavily ionizing particles with the silicon avalanche photodiodes used for the ECAL readout are removed [39]. Background from ECAL showers induced by hadrons are rejected using the ratio of HCAL over ECAL energy inside a cone of radius ∆R = √ (∆η) 2 + (∆φ) 2 = 0.15 around the photon candidate [39,40]. Background contributions from fragmentation and decay photons are rejected by imposing isolation ...