Thread: Function Question

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Question Function Question

    in this simple example of a function, the author of the book explains quite a bit on it. maybe a little too much so i got a little confused. i understand that the first part (test function, return 0 ) defines the way this function will output results on the screen, and the second part introduses the formula and values for the function to work with. am i anywhere near the ballpark, or did i end up in a museum ?

    #include <stdio.h>
    /* test power function */
    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;
    }

    /* power: raise base to n-th power; n >= 0 */
    int power(int base, int n)
    {
    int i, p;
    p=1;
    for (i=1; i<=n; ++i)
    p=p*base;
    return p;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The 'main' function prints 10 lines to the screen.

    Each line prints out three things:
    1) the value of I
    2) the value of 2 raised to the I power.
    3) the value of -3 raised to the I power.

    The 'pow' function actually does the work of raising the base to whatever power your pass it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM