Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.


macrumors 6502a
Original poster
Oct 21, 2011
Johannesburg, South Africa
Hey guys,

So, I have started re-reading a book for iOS programming. I already read a lot of this book - but never took the time to 100% understand every bit of code / challenge - this was a mistake. So I stopped and have restarted.

Book: Objective-C Programming: The Big Nerd Ranch Guide
Author: Aaron Hillegass
Page: 98
Chapter: 5.

It's the Beer Song code.

What I would like to know is:

I know that inside of main.c - I am called the function signTheSong. I understand how that part works - what I want to know is why do I call the fuction again, within the function and how does this code appear to loop ( It counts from 99 to 0) when there is no for / while loop code present?

Lastly - how does the code for "Putting it in the bin, % bottles in the bin" work?

#include <stdio.h>

void singTheSong (int numberOfBottles)
    if (numberOfBottles == 0){
        printf ( "There are sinply no more bottles of beer on the wall\n");
    }else {
        printf ( "%d bottles of beer on the wall, %d bottles of beer\n", numberOfBottles, numberOfBottles);
    int oneFewer = numberOfBottles - 1;
    printf("Take one down, pass it around, %d bottles of beer on the wall\n", oneFewer);
    printf ( "Put a bottle in the bin, %d bottles in the bin\n", numberOfBottles);
int main(int argc, const char * argv[])

    singTheSong(99); //This calles the singTheSong function
    return 0; 

I guess what I am trying to understand is the flow of the code - I understand it as this:

main kicks off "signTheSong.
signTheSong function carries out the If / else statement - does this if / else continue to execute as long as the value is true? (There are still bottles left and "numberOfBottles" is not equal to zero )

I would appreciate any help in understanding this code. :eek:


macrumors member
Aug 8, 2011
It's not that it continues to execute. The main point there is that it's calling itself.

Check out those two links above.

EDIT: As for how the putting the bottles in the bin section works, think back a little. I seem to remember that the function stack is mentioned earlier in that book. Think about how each function would be completed before being popped from the stack.
Last edited:


macrumors 68000
Sep 8, 2006
You should also take a look at the indentation for easier code reading.

I had to take a second look at the code because I thought your else only had 1 line of code. It looked like this would create an infinite loop if you were calling the singTheSong function again outside of that loop. Then I noticed your close bracket.


macrumors 6502a
Original poster
Oct 21, 2011
Johannesburg, South Africa
Thanks for the help guys, much appreciated it. Getting to understand how it works now. :D

Larswik - Yeah, I know you have told me before to use indentation - I'm getting there! :p
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.