PDA

View Full Version : Would like to understand this code 100%




Tander
Aug 21, 2012, 07:45 AM
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);

singTheSong(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. :o



Reason077
Aug 21, 2012, 07:51 AM
http://en.wikipedia.org/wiki/Recursion

balamw
Aug 21, 2012, 07:57 AM
Also: http://forums.macrumors.com/showthread.php?t=1294295

B

iHutch105
Aug 21, 2012, 10:15 AM
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.

larswik
Aug 21, 2012, 03:08 PM
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.

Tander
Aug 22, 2012, 07:57 AM
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

phr0ze
Aug 22, 2012, 12:07 PM
...the function signTheSong...main kicks off "signTheSong. ... signTheSong function

Little bits of detail like this will make your journey so painful. Pay attention to every character.

balamw
Aug 22, 2012, 03:26 PM
Little bits of detail like this will make your journey so painful. Pay attention to every character.

@Tander: In case you didn't catch it. phr0ze means that you replaced sing with sign

B

Tander
Aug 23, 2012, 01:23 AM
Opps. :o

Thanks guys