Subfunctions returning different types together (in C)

  • Thread starter Thread starter muzihc
  • Start date Start date
AI Thread Summary
The discussion centers on a C programming issue where a subfunction, `calculate`, needs to return both an integer sum and a float quotient from two separate functions, `findSum` and `findQuotient`. The main challenge arises from the fact that C functions can only return a single value, leading to confusion and errors in the code. The solution proposed involves using pointers to allow `calculate` to modify the variables for sum and quotient directly. Additionally, there are syntax errors and misnamed functions in the provided code that need correction for proper functionality. Overall, the key takeaway is the importance of using pointers for returning multiple values from a function in C.
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.
 
Technology 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top