Thread: Power function

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Power function

    Hey guys, this is a realy simple question (I think). So I'm supposed to write a function that elevates a number to some other number (function inputs). Here it is as presented in the solution:

    Code:
    float pot(float x, int n){
    
    
        float res;
        int i;
    
    
        for(i=1, res=1.0; i<=n; i++)
            res*=x;
        return res;
    }
    The only question that I have is, if n=0 the output is 1 (as it should be!). Where in this code does that happen?

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Since you have initialized in for loop res = 1.0.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    So, because n<i it just returns the value with what it was initialized with?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Miguel Leão View Post
    So, because n<i it just returns the value with what it was initialized with?
    Exactly.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Power function
    By LaurentiuMatei in forum C Programming
    Replies: 4
    Last Post: 04-17-2015, 12:08 PM
  2. power function
    By yjn in forum C Programming
    Replies: 5
    Last Post: 03-16-2012, 08:38 AM
  3. Th power function
    By wiz23 in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2005, 05:56 AM
  4. Power function question...
    By DramaKing in forum C Programming
    Replies: 6
    Last Post: 02-11-2002, 04:59 PM
  5. power function
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-06-2001, 01:11 AM

Tags for this Thread