Subfunctions returning different types together (in C)

  • Thread starter Thread starter muzihc
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the challenge of returning multiple values of different types from a subfunction in C programming. Participants explore how to effectively use function prototypes, pointers, and return types to achieve this, while addressing specific errors encountered in the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a scenario where a subfunction called "calculate" needs to return both an integer and a float from two other subfunctions, "findSum" and "findQuotient".
  • Another participant suggests that a function can only return one value and proposes using pointers to modify the variables for sum and quotient directly within the "calculate" function.
  • A later reply questions whether "calculate" still calls the other functions, expressing confusion about the implementation.
  • Another participant points out a potential naming conflict in the code, where the variable "sum" is used as both a variable name and a function name, which could lead to confusion.

Areas of Agreement / Disagreement

Participants generally agree on the need to use pointers to return multiple values, but there is some confusion regarding the implementation details and the proper function calls. The discussion remains unresolved regarding the best approach to correctly implement the desired functionality.

Contextual Notes

There are limitations in the provided code, including missing function definitions and potential naming conflicts that could lead to errors. The discussion does not resolve these issues.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
Replies
6
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K