### Passing Variables by Value to a Function

Hello all, I was wondering if I could get some help with my code. For some reason, my variables pressure_max1, pressure_min1, temp_max1, and temp_min1 don't seem to be changing or working as I hoped they would. I'm trying to make a program where you enter 20 temperatures and 20 pressures, and it's supposed to find the largest and the smallest of the values. Usually I can work these issues out, but I really need help from the community on this one. My poor, flu-riddled brain is pulling blanks on what's going on.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153`` ``````#include /******************************************************************************/ /* */ /* main */ /* */ /******************************************************************************/ /* */ /* Input Variables(arrays): */ /* float temp[20] - Temperature for the scientific experiment */ /* float pressure[20] - Pressure for scientific experiment */ /* ch - Used for exiting program */ /* */ /* Output Variables (Calculated): */ /* float temp_max - Final max temperature */ /* float pressure_max - Final max pressure */ /* float temp_min - Final min temperature */ /* float pressure_min - Final min pressure */ /* */ /* Functions Called */ /* float temp_extreme() - Finds temp-max and temp_min */ /* float pressure_extreme() - Finds pressure_max and pressure_min */ /* */ /* Variables local to temp_extreme() and pressure_extreme() */ /* */ /* float temp_max1 - Largest temperature in the temp array */ /* float temp_min1 - Smallest number in the temp array */ /* float pressure_max1 - Largest pressure in the pressure array */ /* float pressure_min1 - Smallest pressure in the pressure array */ /* */ /******************************************************************************/ float temp[20]; float pressure[20]; float temp_extreme(float, float, int); float pressure_extreme(float, float, int); int main() { float temp_max = 0; float temp_min = 0; float pressure_max = 0; float pressure_min = 0; int ch; int count = 0; printf("Please input the 20 temperature and pressure readings\n"); for(; count < 20; count++) { printf("Count = %d \n", count); printf("Temperature: "); scanf("%f", &temp[count]); printf("Pressure: "); scanf("%f", &pressure[count]); temp_extreme(temp_max, temp_min, count); pressure_extreme(pressure_max, pressure_min, count); } printf("Lowest Temperature: %f degrees\nHighest Temperature: %f degrees\n Lowest Pressure: %f \n Highest Pressure: %f \n",temp_min, temp_max, pressure_min, pressure_max); while ((ch = getchar()) != '\n' && ch != EOF); { printf ("\n\nPress enter to close the program\n\n"); getchar (); return 0; } return 0; } /******************************************************************************/ /* */ /* float temp_extreme(float, float, int) */ /* */ /******************************************************************************/ /* */ /* Variables passed: */ /* temp_max -> temp_max1 */ /* temp_min -> temp_min1 */ /* */ /******************************************************************************/ float temp_extreme(float temp_max1, float temp_min1, int count1) { /* If-Else Initializes max1 and min1 to the value of temp[count1] */ /* This enables a comparison for after the initial count from main(), setting */ /* the scene for the else statment, which compares temp_max1 and temp_min1 */ /* to the value of the current array element (based on the count of the for */ /* loop in main() */ printf("%d \n", count1); if(count1 = 0) { temp_max1 = temp[count1]; temp_min1 = temp[count1]; printf("inital max: %f\n min: %f \n", temp_max1, temp_min1); } else { if(temp_max1 <= temp[count1]) temp_max1 = temp[count1]; if(temp_min1 >= temp[count1]) temp_min1 = temp[count1]; printf("max: %f\n min: %f \n", temp_max1, temp_min1); return temp_max1, temp_min1; } } /******************************************************************************/ /* */ /* float pressure_extreme(float, float, int) */ /* */ /******************************************************************************/ /* */ /* Variables passed: */ /* pressure_max -> pressure_max1 */ /* pressure_min -> pressure_min1 */ /* */ /******************************************************************************/ float pressure_extreme(float pressure_max1, float pressure_min1, int count1) { printf("%d \n", count1); /* If-Else Initializes max1 and min1 to the value of temp[count1] */ /* This enables a comparison for after the initial count from main(), setting */ /* the scene for the else statment, which compares pressure_max1 and */ /* pressure_min1 to the value of the current array element (based on the */ /* count of the for loop in main() */ if(count1 = 0) { pressure_max1 = pressure[count1]; pressure_min1 = pressure[count1]; printf("Initial max: %f\n min: %f \n", pressure_max1, pressure_min1); } else { if(pressure_max1 <= pressure[count1]) pressure_max1 = pressure[count1]; if(pressure_min1 >= pressure[count1]) pressure_min1 = pressure[count1]; printf("max: %f\n min: %f \n", pressure_max1, pressure_min1); return pressure_max1, pressure_min1; } }``````
` return temp_max1, temp_min1;`

is the same as:

` return temp_min1 ;` due to the way the comma operator works and the fact that you can only return one object from a function.

Your choices, if you want to (effectively) return more than one value from a function are to return a struct or class, return a container or pass variables in by reference and modify them in the function body.

Also, it may be useful to do something with the value returned rather than discard it as you're doing now.
Hmmm...Since I've not yet learned about containers or structs, I might have to pass the variables by reference. That might be easiest. I would set those variables as static, but I honestly don't have need of those values to be retained. I'm only really concerned with the largest and the smallest values.
Topic archived. No new replies allowed.