The adaptability of housing to the resident's needs over time is crucial, relying on the flexibility of the structure, with expanding indoor space to outdoor areas being one of the most suitable methods. On the other hand, outdoor spaces can contribute to providing environmentally compatible housing by reducing energy consumption while ensuring thermal comfort. In flexible and environmental housing, outdoor space (courtyard) plays a crucial role. This research focused on investigating the role of the courtyard in apartment housing from two perspectives: flexibility and environmental compatibility. In flexible and environmental housing, outdoor space (courtyard) plays a crucial role. This research focused on investigating the role of courtyards in apartment housing from two perspectives: flexibility and environmental compatibility. In this regard, the flexibility approach involved expanding housing areas into the courtyard, while the environmental approach entailed determining the optimal direction and position for the courtyard. Environmental parameters such as energy consumption, thermal comfort, and solar radiation were simulated in three selected time intervals using EnergyPlus software. The validation process involved comparing the measurement data with the TES-132 data logger and simulation data. The optimal unit was identified using variance analysis and post hoc testing. Subsequently, the flexibility technique was applied to the optimal unit, and the energy consumption and thermal comfort parameters were compared before and after the implementation. The case study involved three flexible housing units with courtyards in the corners (A), the north and south (B), and the east and west (C) of buildings in a cold climate region in Hamedan. The energy consumption and thermal comfort results in the NW, NE, and SW directions showed similarities across all units. Therefore, the optimal unit for these directions was determined through variance analysis of solar radiation. The solar radiation results on the main walls and courtyards indicated that the courtyard acted as a climatic modifier, compensating for excess and deficiency of solar radiation. The post hoc T-test analysis on solar radiation for the courtyards demonstrated that the optimal unit was assumed to be BNW, CNE, and BSW, while in the SE direction, with all three environmental parameters matching, unit ASE was identified as the optimal one. After implementing flexibility in the optimal unit, the comparison results before and after expansion showed a reduction of 11.7% in energy consumption per capita and 6% in thermal comfort. Flexibility, accompanied by environmental efficiency, ensured that the courtyard continued to serve as a climate regulator and remained environmentally after the expansion of units.