Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Subfunctions returning different types together (in C)

  1. Oct 6, 2008 #1
    (this is basically a dumbed-down version for the sake of clarity, but gives the same problem I'm getting)

    I have a subfunction called by main, named calculate, which calls two other subfunctions ("findSum" and "findQuotient") and then returns their data to main. "findSum" calculates the sum of two numbers and then returns its data to calculate. "findQuotient" does the same, except it returns the quotient instead. The difficulty is that "findSum" is an integer and "findQuotient" is a float. So if I wanted to return both of these using calculate, what would my prototype and function declaration, etc. have to look like? If it needs pointers, I'd appreciate some advice since I haven't mastered those yet.

    My error message is "In function calculate: warning: return makes integer from pointer without a cast"

    Here's an example (input/output to user removed for ease):

    =======================================================

    #include <stdio.h>
    int calculate (int integer1, int integer2);
    int findSum(int integer1, int integer2);
    float findQuotient(float integer1, float integer2);

    int main(void)
    {
    int integer1, integer2;
    int sum;
    float quotient;

    calculate(integer1, integer2)
    return 0;
    }

    int calculate(integer1, integer2)
    {
    sum(integer1, integer2);
    divide(integer1, integer2);

    return sum, divide;
    }

    int findSum(integer1, integer2)
    {
    int sum;
    sum = integer1 + integer2;
    return sum;
    }

    float findQuotient(integer1, integer2)
    {
    float quotient;
    quotient = (float)integer1 / integer2;
    return quotient;
    }



    =============================================

    I just typed this up, so please ignore any syntax errors, etc. I appreciate the help, thanks.
     
  2. jcsd
  3. Oct 7, 2008 #2

    KTC

    User Avatar

    A function can only return one value. You can (among other options), pass in a pointer to sum & quotient, and have your calculate() directly modify those 2 variables assigning it the answers.

    Code (Text):
    #include <stdio.h>
    int calculate (int integer1, int integer2);
    int findSum(int integer1, int integer2);
    float findQuotient(float integer1, float integer2);

    int main(void)
    {
        int integer1, integer2;
        int sum;
        float quotient;

        calculate(integer1, integer2, &sum, &quotient)
        return 0;
    }

    void calculate(int integer1, int integer2, int *sum, float *quotient)
    {
        *sum = sum(integer1, integer2);
        *quotient = divide(integer1, integer2);
    }

    /* ...... */
    Which works fine.... except you haven't defined a divide() function, and your findQuotient() function doesn't do what you think it does.
     
  4. Oct 7, 2008 #3
    Does calculate still call the other functions? That's what I need to do. I'm lost.
     
  5. Oct 7, 2008 #4

    KTC

    User Avatar

    Look at the code I posted. The definition of calculate explicitly called sum() & divide(). Which um, I just realized is a problem as sum is a int variable, and not the name of your function. But anyhow, yes the function suppose to call two different function and assigned their respective return value to a variable.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Subfunctions returning different types together (in C)
  1. Return type void (Replies: 1)

Loading...