Recursion

Can a function call itself? You bet! We call that recursion. There is a notoriously dull song called 99 Bottles of Beer. Create a new C Command Line Tool named BeerSong. Open main.c and add a function to write out the words to this song and then kick it off in main():

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​

v​o​i​d​ ​s​i​n​g​S​o​n​g​F​o​r​(​i​n​t​ ​n​u​m​b​e​r​O​f​B​o​t​t​l​e​s​)​ {​ ​ ​ ​ ​i​f​ ​(​n​u​m​b​e​r​O​f​B​o​t​t​l​e​s​ ​=​=​ ​0​)​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​r​e​ ​a​r​e​ ​s​i​m​p​l​y​ ​n​o​ ​m​o​r​e​ ​b​o​t​t​l​e​s​ ​o​f​ ​b​e​e​r​ ​o​n​ ​t​h​e​ ​w​a​l​l​.​\​n​\​n​"​)​;​ ​ ​ ​ ​}​ ​e​l​s​e​ ​{​ ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​d​ ​b​o​t​t​l​e​s​ ​o​f​ ​b​e​e​r​ ​o​n​ ​t​h​e​ ​w​a​l​l​.​ ​%​d​ ​b​o​t​t​l​e​s​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.