Thread: problem with return value in C

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    problem with return value in C

    Greetings all.

    I am running into an issue with a program I am working with. In the power function everything seems to be working but it returns some funky value.

    Any help would be great.


    Code:
    
    
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    void run(void);
    int getnumb2(void);
    int getnumb1(void);
    int power( int semisol, int number, int to );
    
    int main(){
    
    run();
    
    
    
    return (0);
    }
    
    void run(void){
    int x, y, answer;
    printf("\nThis program will take a number and an exponent. For example, the program will preform x to the power of y.");
    x=getnumb1();//gets the first the number; the base
    y=getnumb2();//gets the exponent
    answer=power(x, x, y);//a recursive function that solves the equation// 
    
    printf("\n%d to the power of %d is %d\n\n\n",x,y,answer);
    }
    
    int getnumb1(void){//gets the first the number; the base
    int x;
    printf("\nPlease enter a value for x: ");
    scanf("%d",&x);
    return x;
    }
    
    int getnumb2(void){//gets the exponent
    int y;
    printf("\nPlease enter a value for y: ");
    scanf("%d",&y);
    return y;
    }
    
    int power(int semisol, int number, int to){//a recursive function that solves the equation/ x to the power of y or 'number' to the power of 'to'
    
    if (to==0){
    semisol=1;
    printf("test of zero");//tst lones
    return semisol;
    }
    else if (to==1){
    printf("%d %d",to,semisol);//test line only
    printf(" test of one ");//test line only
    return semisol;
    }
    else if (to>1){
    printf("%d %d",to,semisol);//test line only
    printf(" Stop ");//test line only
    semisol= semisol * number;
    to = to - 1;
    power(semisol, number,  to);
    }
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    power(semisol, number,  to);
    So if you don't return the answer.... ("return" is not magic -- and it doesn't mean "return to main". It means "return to the function that called this one". So when you are writing a recursive function, return will only go back one level, not all the way to the beginning.)

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you provide the input you are using and what is being printed out?

    Also, you have two functions that do the same thing. Why not just call the same function twice, once for the base, once for the argument?

    Ok, tabstop is right, he beat me to it

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    So, how would I go about getting that value back to the other function that called it?
    Last edited by jericjones45; 03-27-2010 at 10:25 AM.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    return a value

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    If I return a value than it will kind of defeat the purpose of the function. I had the statement return semisol which is returning an integer value.

    I am sorry. I am confused.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    The 'return' statement returns to the function that called it. So if you call 'Power' within 'Power', when you return it will return to 'Power', until the point where the function that called it was not 'Power', at which point it will return to that.

    A simple example might help to illustrate.

    (If you don't know, factorial of 4 is 4*3*2*1, factorial of 3 is 3*2*1 etc).

    Code:
    int factorial( int num )     //Returns the factorial of num
    {
            if( num == 0 )     //num == 0 used for clarity
            {
                    return 1;
            }
            else
            {
                    return num * factorial( num - 1 );
            }
    }
    As you can see, if 'num' is equal to 0, the function returns 1 (factorial of 0 is 1). If it is above zero, it will multiply the current number by the factorial of the number below it. If it keeps doing this, 'num' will eventually be 0, so the function will return 1 to the function that called it, which will return num * 1 to the function that called that, and so on.

    Recursion is a rather difficult thing to explain. Your best bet is to look for examples (the one shown above is probably the simplest you will find) and try to understand them.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jericjones45 View Post
    If I return a value than it will kind of defeat the purpose of the function.
    The purpose of the function is to return a value. If you don't return a value, it will definitely defeat the purpose of the function. And if you don't have a statement marked "return" in that branch, the function won't return.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    Thanks for your help. I will try to analyze recursion a little more and give it another shot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM