O'Reilly logo

Objective-C Programming: The Big Nerd Ranch Guide by Mikey Ward, Aaron Hillegass

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required