Thread: strange strange functions

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    strange strange functions

    hi
    actually im reading a book about C.
    in this book there is a lesson called functions.
    i guess that n is going to be the exponent of m so far.

    the following code is directly from the book:

    Code:
    #include<stdio.h>
    
    int power ( int m, int n);
    
    main ()
    {
       int i;
       for (i=0; i<10;++i)
       	printf("%d %d %d\n", i, power(2, i), power(-3, i));
       return 0;
    }
    int power(int base, int n)
    {
       int i, p;
    
       p=1;
       for(i=n;i<=n;++i)
       	p = p * base;
       return p;
    }
    i need help with this code because i dont understand it.
    an explanation of the whole code line by line would be very appreciated.

    threadhead

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Line by line explanation?! Come on, there must be some of it you understand already. What do you think it's doing?

    There's a bug in it too, I'm afraid, on this line:
    >>for(i=n;i<=n;++i)
    I'll let you see if you can find it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i found the bug hammer

    this for statement is useless.
    because 'i' is setted equal to n so he cant enhance the value.


    i still dont understand why there are other vars in the prototype than in the function. shouldnt they be the same?

    i dont understand why to take excatly these variables like i, p, base and n.
    and why has the function power to return p??
    doesnt the following line declare that ie 2 has the exponent i and -3 too? so only the output is written down in main and the arithmetic is declared in power.

    you see the more i write about functions the more i understand them, keep helping me
    Code:
    printf("%d %d %d\n", i, power(2, i), power(-3, i));
    another question would be, why p is declared as 'p * base' in power. i thought parameter names were local to power and not visible to other functions....

    sorry to be a bit annoying, but if i am not annoying to you, ill never understand this.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The variable names used in the function prototype don't have to be the same as those use on the function itself, although it's bad style to mix them. It's the type of variable that's important, in this case it's 2 ints. This means you can ignore m.

    i is used as a loop counter only.
    p is used to collate the result of the power calculation. For example:
    2*2*2=8
    ... is done in stages, so the first time through the loop, p is 1 and the calc looks like this:
    >p = p * base;
    >p = 1 * 2; /* so now p is 2 */
    ... and the next time...
    >p = 2 * 2; /* so now p is 4 */
    ... and the next time...
    >p = 4 * 2; /* so now p is 8 */
    ... and when we've looped enough, we simply return the value to the caller:
    >return p;

    Any help?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    that was a big step forward for me

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  2. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM