Subfunctions returning different types together (in C)

  • Thread starter Thread starter muzihc
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 4K views
muzihc
Messages
15
Reaction score
0
(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.
 
Physics news on Phys.org
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:
#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.
 
Does calculate still call the other functions? That's what I need to do. I'm lost.
 
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.