subfunctions returning different types together

This is a discussion on subfunctions returning different types together within the C Programming forums, part of the General Programming Boards category; Basically the goal of this program is to have main call a function that prompts the user for two variables, ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    subfunctions returning different types together

    Basically the goal of this program is to have main call a function that prompts the user for two variables, then call a function that handles different calculations, and then call a function that displays back to user. The function that does the calculations, called calc, has 3 subfunctions that it calls and then retrieves data from and sends back to main.

    My problem is getting calc to send it's data back to main. Specifically, I think the problem is that I don't know how to get calc to take both intOps and doubleOps and return them to main since intOps are int values and doubleOps are double values. I get this error message:

    In function "calc":
    warning: return makes integer from pointer without a cast


    I've checked my notes, text, etc. but haven't found anything that deals with this problem. I'd greatly appreciate any help, thanks.



    Code:
    #include <stdio.h>
    
    int getInput(void);
    int calc(int one, int two);
    int intOps(int one, int two);
    float doubleOps(int one, int two);
    int algebra(int one, int two);
    void display(int one, int two, int sum, double half1, double half2,
    int quotient, int remainder, double fraction, int algebraSolution);
    
    int main(void)
    {
       int one, two;
       double half1, half2, fraction;
       int sum, quotient, remainder;
       int algebraSolution;
    
       getInput();
       calc(one, two);
       display(one, two, sum, half1, half2, quotient, remainder,
       fraction, algebraSolution);
    
       return 0;
    }
    
    int getInput(void)
    {
       int one, two;
       
       printf("\nPlease enter two integers ");
       scanf("%d%d", &one, &two);
    
       return one, two;
    }
       
    int calc(int one, int two)
    {
       intOps(one, two);
       doubleOps(one, two);
       algebra(one, two);
    
       return intOps, doubleOps, algebra;
    }
       
    int intOps(int one, int two)
    {
       int sum, quotient, remainder;
       
       sum = one + two;
       quotient = one / two;
       remainder = one % two;
       
       return sum, quotient, remainder;
    }  
       
    float doubleOps(int one, int two)
    {
       double half1, half2, fraction;
       
       half1 = (double)one / 2.;
       half2 = (double)one / 2.;
       fraction = (double)one / two;
     
       return half1, half2, fraction;
    }  
       
    int algebra(int one, int two)
    {
       int algebraSolution;
       
       algebraSolution = 2 * one + 4 * two + one * two - one / two;
       
       return algebraSolution;
    }
    
    void display(int one, int two, int sum, double half1, double half2,
    int quotient, int remainder, double fraction, int algebraSolution)
    {  
       printf("\n%20s%20d", "Integer 1", one);
       printf("\n%20s%20d", "Integer 2", two);
       printf("\n%20s%20d", "Sum", sum);
       printf("\n%20s%20.1f", "Half of Integer 1", half1);
       printf("\n%20s%20.1f", "Half of Integer 2", half2);
       printf("\n%20s%20d", "Quotient", quotient);
       printf("\n%20s%20d", "Remainder", remainder);
       printf("\n%20s%20.4f", "Fraction", fraction);
       printf("\n%20s%20d", "Algebra Solution", algebraSolution);
       printf("\n\n");
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That isn't how you return something, now is it.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by master5001 View Post
    That isn't how you return something, now is it.
    That's what I want to know. I'm new at C, I need someone to show me what to change.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    return intOps, doubleOps, algebra;
    You can't return more than one variable type from a function. To connect functions together with more than one variable make them global.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The only way to do this is to pass in parameters by reference or to return a structure.

    The Former:
    Code:
    void doubleOps(int one, int two, float *half1, float *half2, float *fraction)
    { 
       *half1 = (double)one / 2.;
       *half2 = (double)one / 2.;
       *fraction = (double)one / two;
    }
    The Latter:
    Code:
    struct number
    {
      float half[2], fraction;
    };
    
    struct number doubleOps(int one, int two)
    {
       struct number obj;
       
       obj.half[0] = (double)one / 2.;
       obj.half[1] = (double)one / 2.;
       obj.fraction = (double)one / two;
     
       return obj;
    }

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by itCbitC View Post
    Code:
    return intOps, doubleOps, algebra;
    You can't return more than one object type from a function. To connect functions together with more than one object make the variables global.
    That would be like my last way of doing this. And keep in mind the use of globals is only my suggestion when Hitler is standing behind me with a lugar to my head.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I should also point out that the order in which I demonstrated how to do this is the order in which I would tend to favor. If you pass by reference you are passing a physical variable from one function to another, not just the information stored in that variable.

    So saying doubleOps(5, 6, &x, &y, &z) passes the actual address to the variable at x, y and z respectively. So changes to these variables within the called function actually change the values of these variables instead of passing in a copy of what the respective variables contain. Meh... Elysia explains this better... Though she will nit-pick my terminology.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Choice depends on how return variables are used but calling by reference is by far the best. Globals are fine as long as one keeps track of changes to them or they could get clobbered.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Thanks guys.

    Quote Originally Posted by master5001 View Post
    The only way to do this is to pass in parameters by reference or to return a structure.

    The Former:
    Code:
    void doubleOps(int one, int two, float *half1, float *half2, float *fraction)
    { 
       *half1 = (double)one / 2.;
       *half2 = (double)one / 2.;
       *fraction = (double)one / two;
    }
    If I were to go with this, what should I change in the rest of my program to make this work?

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your globals would probably not results in a thread safe solution. Unlike passing by reference which could be more thread safe.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have to go... but until tabstop jumps in to answer more thoroughly:

    Code:
    calc(one, two, &half1, &half2, &fraction);

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    I've put in these two:

    Code:
    void doubleOps(int one, int two, float *half1, float *half2, float *fraction)
    { 
       *half1 = (double)one / 2.;
       *half2 = (double)one / 2.;
       *fraction = (double)one / two;
    }
    Code:
    calc(one, two, &half1, &half2, &fraction);
    Based on my error messages and what I think, I'm guessing the problems are with my function declaration for doubleOps:

    Code:
    float doubleOps(int one, int two);
    ...and my calc function:

    Code:
    int calc(int one, int two)
    {
       intOps(one, two);
       doubleOps(one, two);
       algebra(one, two);
    }
    Anyone know what needs to be done, or are these even the source of the problem?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't have two functions with the same name in C. Which one do you have, which one do you want, and we'll go from there.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    You can't have two functions with the same name in C. Which one do you have, which one do you want, and we'll go from there.
    I don't see where I have two functions with the same name?

    Here is my code as of now:
    Code:
    #include <stdio.h>
    
    //function declarations
    int getInput(void);
    int calc(int one, int two);
    int intOps(int one, int two);
    float doubleOps(int one, int two);
    int algebra(int one, int two);
    void display(int one, int two, int sum, double half1, double half2,
    int quotient, int remainder, double fraction, int algebraSolution);
    
    
    int main(void)
    {
       //local declarations
       int one, two;
       double half1, half2, fraction;
       int sum, quotient, remainder;
       int algebraSolution;
    
       //statements
       getInput();
       calc(one, two, &half1, &half2, &fraction);
       display(one, two, sum, half1, half2, quotient, remainder,
       fraction, algebraSolution);
    
       return 0;
    }
    
    int getInput(void)
    {
       int one, two;
       
       printf("\nPlease enter two integers ");
       scanf("%d%d", &one, &two);
       
       return one, two;
    }
       
    int calc(int one, int two)
    {
       intOps(one, two);
       doubleOps(one, two);
       algebra(one, two);
    }
    
    int intOps(int one, int two)
    {
       int sum, quotient, remainder;
       
       sum = one + two;
       quotient = one / two;
       remainder = one % two;
       
       return sum, quotient, remainder;
    }
    
    void doubleOps(int one, int two, float *half1, float *half2, float *fraction)
    {
       *half1 = (double)one / 2.;
       *half2 = (double)one / 2.;
       *fraction = (double)one / two;
    }
    
    int algebra(int one, int two)
    {
       int algebraSolution;
       
       algebraSolution = 2 * one + 4 * two + one * two - one / two;
       
       return algebraSolution;
    }
     
    void display(int one, int two, int sum, double half1, double half2,
    int quotient, int remainder, double fraction, int algebraSolution)
    {
       printf("\n%20s%20d", "Integer 1", one);
       printf("\n%20s%20d", "Integer 2", two);
       printf("\n%20s%20d", "Sum", sum);
       printf("\n%20s%20.1f", "Half of Integer 1", half1);
       printf("\n%20s%20.1f", "Half of Integer 2", half2);
       printf("\n%20s%20d", "Quotient", quotient);
       printf("\n%20s%20d", "Remainder", remainder);
       printf("\n%20s%20.4f", "Fraction", fraction);
       printf("\n%20s%20d", "Algebra Solution", algebraSolution);
       printf("\n\n");
    }
    My error messages:

    In function `main':
    error: too many arguments to function `calc'
    At top level:
    error: conflicting types for 'doubleOps'
    error: previous declaration of 'doubleOps' was here
    error: conflicting types for 'doubleOps'
    error: previous declaration of 'doubleOps' was here

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by muzihc View Post
    I don't see where I have two functions with the same name?

    Here is my code as of now:
    Code:
    #include <stdio.h>
    
    //function declarations
    int getInput(void);
    int calc(int one, int two);
    int intOps(int one, int two);
    float doubleOps(int one, int two);
    int algebra(int one, int two);
    void display(int one, int two, int sum, double half1, double half2,
    int quotient, int remainder, double fraction, int algebraSolution);
    
    
    int main(void)
    {
       //local declarations
       int one, two;
       double half1, half2, fraction;
       int sum, quotient, remainder;
       int algebraSolution;
    
       //statements
       getInput();
       calc(one, two, &half1, &half2, &fraction);
       display(one, two, sum, half1, half2, quotient, remainder,
       fraction, algebraSolution);
    
       return 0;
    }
    
    int getInput(void)
    {
       int one, two;
       
       printf("\nPlease enter two integers ");
       scanf("&#37;d%d", &one, &two);
       
       return one, two;
    }
       
    int calc(int one, int two)
    {
       intOps(one, two);
       doubleOps(one, two);
       algebra(one, two);
    }
    
    int intOps(int one, int two)
    {
       int sum, quotient, remainder;
       
       sum = one + two;
       quotient = one / two;
       remainder = one % two;
       
       return sum, quotient, remainder;
    }
    
    void doubleOps(int one, int two, float *half1, float *half2, float *fraction)
    {
       *half1 = (double)one / 2.;
       *half2 = (double)one / 2.;
       *fraction = (double)one / two;
    }
    
    int algebra(int one, int two)
    {
       int algebraSolution;
       
       algebraSolution = 2 * one + 4 * two + one * two - one / two;
       
       return algebraSolution;
    }
     
    void display(int one, int two, int sum, double half1, double half2,
    int quotient, int remainder, double fraction, int algebraSolution)
    {
       printf("\n%20s%20d", "Integer 1", one);
       printf("\n%20s%20d", "Integer 2", two);
       printf("\n%20s%20d", "Sum", sum);
       printf("\n%20s%20.1f", "Half of Integer 1", half1);
       printf("\n%20s%20.1f", "Half of Integer 2", half2);
       printf("\n%20s%20d", "Quotient", quotient);
       printf("\n%20s%20d", "Remainder", remainder);
       printf("\n%20s%20.4f", "Fraction", fraction);
       printf("\n%20s%20d", "Algebra Solution", algebraSolution);
       printf("\n\n");
    }
    My error messages:

    In function `main':
    error: too many arguments to function `calc'
    At top level:
    error: conflicting types for 'doubleOps'
    error: previous declaration of 'doubleOps' was here
    error: conflicting types for 'doubleOps'
    error: previous declaration of 'doubleOps' was here
    And your compiler gives you the line numbers at the end too. (Edit: Yes, the first one is a prototype, but it's a prototype for a completely different function than the one you wrote.)

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 11:57 AM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21