Thread: subfunctions returning different types together

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Ok I fixed the problem that you put in red, specifically I changed

    Code:
    float doubleOps(int one, int two);
    in function declarations to
    Code:
    void doubleOps(int one, int two, float *half1, float *half2, float *fraction)
    What about these two errors?

    In function `main':
    29: error: too many arguments to function `calc'
    In function `calc':
    50: error: too few arguments to function `doubleOps'

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    calc only takes two arguments, but you try to give it five. Conversely doubleOps takes five arguments, but you try to give it two. Maybe you want to change calc to take five arguments too? Then you could pass those extra arguments to doubleOps.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    calc only takes two arguments, but you try to give it five. Conversely doubleOps takes five arguments, but you try to give it two. Maybe you want to change calc to take five arguments too? Then you could pass those extra arguments to doubleOps.
    If I wanted calc to take 5 arguments, which 3 arguments would I add? I've experimented with it but always end up with errors.

    Then you could pass those extra arguments to doubleOps.
    And I'm not sure how I'd go about doing that.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by muzihc View Post
    If I wanted calc to take 5 arguments, which 3 arguments would I add?
    Which three arguments would you add? The three you already added?
    Code:
     calc(one, two, &half1, &half2, &fraction);
    means that calc had better look like
    Code:
    void calc(int one, int two, double *half1, double *half2, double *fraction);
    Notice that you have to choose between double and float. In your main, you chose doubles, so that's what I used. But your other functions all have to match that.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Well, I took it a new direction which matches my IPO better. I completely cut out the excess arguments, so now they all have 2 arguments except the last function (display). No matter what I tried, I'd always got more errors, even when I followed what you said exactly. It compiles, but it doesn't give the correct values. It displays a large value for the second number it got from user and 0 for everything else that is supposed to display after taking two numbers from the user and doing a series of calculations. Anyone see what I must do to make it work properly?

    Code:
    #include <stdio.h>
       
    //function declarations
    int getInput(void);
    void calc(int one, int two);
    int intOps(int one, int two);
    void 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);
       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;
    }
       
    void 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)
    {
       double half1, half2, 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");
    }

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just because you have variables of the same name in different functions doesn't mean that they have anything whatsoever to do with each other. None of your "half1"s refer to the same variable, anywhere.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    Just because you have variables of the same name in different functions doesn't mean that they have anything whatsoever to do with each other. None of your "half1"s refer to the same variable, anywhere.
    And how do I fix this ?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's why we told you to pass things in: everything you want the function to know about, you have to pass in.

  9. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    That's why we told you to pass things in: everything you want the function to know about, you have to pass in.
    I don't know how to pass in. Is this right?

    Code:
    #include <stdio.h>
    int calc(int one, int two);
    
    
    int main()
    {
        calc (*pcalc)(one, two);
        pcalc = calc
    
        return 0;
    }
    
    int calc(int one, int two);
    {
       //What would go in this function
    }

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Given what you've got, there's nothing you can put in the //What would go in this function bit that would satisfy the constraints of the problem as you posted it in the original post. To "return" multiple values from the function back to main, you must pass in pointers to hold those values. It is not an option.

  11. #26
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    Given what you've got, there's nothing you can put in the //What would go in this function bit that would satisfy the constraints of the problem as you posted it in the original post. To "return" multiple values from the function back to main, you must pass in pointers to hold those values. It is not an option.
    I understand that, I just don't know how to work with pointers. Think you could give me an example?

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You had a half-dozen earlier in the thread; I don't know what I can do differently. Which part of them didn't you understand?

  13. #28
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    You had a half-dozen earlier in the thread; I don't know what I can do differently. Which part of them didn't you understand?
    okay on this example

    Code:
    void doubleOps(int one, int two, double *half1, double *half2, double *fraction)
    { 
       *half1 = (double)one / 2.;
       *half2 = (double)one / 2.;
       *fraction = (double)one / two;
    }
    1) That's what my function should look like with pointers. I understand that. Does that mean my function declaration should read:

    Code:
    void doubleOps(int one, int two, double *half1, double *half2, double *fraction);
    2) How should it be called in main, like this?

    Code:
    calc(one, two, &half1, &half2, &fraction);
    3) Does this mean every time I use half1 throughout the program it needs to be with a pointer?

    4) Would I need to change my calc function? If yes, I don't what to do

    Code:
    int calc(int one, int two)
    {
       intOps(one, two);
       doubleOps(one, two);
       algebra(one, two);
    }

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, sort of, only when you need to pass it to a function that changes it, and yes.

    Calc needs to take even more arguments, since you also have sum, and quotient, and remainder, and algebraSolution. Calc will take them all, call intOps with the appropriate five, call doubleOps with the appropriate five, etc.

  15. #30
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    I think I got my pointers down. I just get these 5 syntax errors now, which I'm not sure how to resolve:

    10: error: syntax error before '*' token
    49: error: syntax error before '*' token
    In function `calc':
    53: error: syntax error before "int"
    54: error: syntax error before "int"
    55: error: syntax error before "int"


    Code:
    #include <stdio.h>
    
    //function declarations
    int getInput(void);
    void calc(int one, int two, int *sum, int *quotient, *int remainder,
    double *half1, double *half2, double *fraction, int *algebra
    Solution);
    int intOps(int one, int two, int *sum, int *quotient, int *remainder);
    void doubleOps(int one, int two, double *half1, double *half2, double *fraction);
    int algebra(int one, int two, int *algebraSolution);
    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, &sum, &quotient, &remainder, &half1, &half2, &fraction,   &algebraSolution);
       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;
    }
       
    void calc(int one, int two, int *sum, int *quotient, *int remainder,
    double *half1, double *half2, double *fraction, int *algebra
    Solution)
    {
       intOps(int one, int two, int *sum, int *quotient, int *remainder);
       doubleOps(int one, int two, double *half1, double *half2, double *fraction);
       algebra(int one, int two, int *algebraSolution);
    }
       
    int intOps(int one, int two, int *sum, int *quotient, int *remainder)
    {
       *sum = one + two;
       *quotient = one / two;
       *remainder = one % two;
    }
     
    void doubleOps(int one, int two, double *half1, double *half2, double *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;
    }
     
    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");
    }

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, 12:57 PM
  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, 11:04 PM