# Thread: passing variables to functions?

1. ## passing variables to functions?

my question is, i recieved a float from getData which is stored in cost Sqf. then i send it to the function calculate, and then i print it in the calsulate function and it comes out wrong (i.e. i would type in 22.23 in getData, then in calculate costSqf would = 2.00 when printed)

how do i send the float to calculate so it would = 22.23, the value that main is holding?

Code:
```int main (void)
{

int roomLth;
int roomWth;
int custDsc;
float costSqf;
float install;
float subTotal;
float total;

/*Statements*/
getData(&roomLth, &roomWth, &custDsc, &costSqf);
printf("%6.2f is costSqf", costSqf);
calculate(roomLth, roomWth, custDsc, costSqf, &install,
&subTotal, &total);
/*
printResult(total);
*/
return 0;
}
/*===================================
calculate function
=====================================*/
void calculate (int roomLth,
int roomWth,
int custDsc,
float costSqf,
float *install,
float *subTotal,
float *total)
{

/*statements*/

int a = roomLth;
int b = roomWth;
float c = costSqf;
printf("\n%f is roomWth", costSqf);
g = calcInstall (a, b, c);
printf("\n %6.2f test\n", g);
e = calcSubTotal (g, d);
printf("%6.2f e \n", e);
/*      calcTotal (e, &f);

*install = g;
*subTotal = e;
*total = f;
*/
return;
}```
thanks and if anyone could help it would be appreciated.
thanks

2. does this statement print the right value in function calculate()?

printf("\n%f is roomWth", costSqf);

3. no that is the thing, it does not hold the right value

if in get data for costSqf i typed

22.23

in that print statement you just mentioned it would print

2.00

here is the code i put into one file, take a look please.

Code:
```/*chad.h*/
#include <stdio.h>

/*Prototype Declarations*/

#define lab_Cost    0.35
#define taxRate     0.085

/*main*/

int main (void)
{
int roomLth;
int roomWth;
int custDsc;
float costSqf;
float install;
float subTotal;
float total;

/*Statements*/

getData(&roomLth, &roomWth, &custDsc, &costSqf);
printf("%6.2f is costSqf", costSqf);
calculate(roomLth, roomWth, custDsc, costSqf, &install,
&subTotal, &total);
/*
printResult(total);
*/
return 0;
}

/*getData*/
void getData (int *roomLth,
int *roomWth,
int *custDsc,
float *costSqf)
{
printf("Length of room (feet)?  ");
scanf("%d", roomLth);

printf("Width of room (feet)?  ");
scanf("%d", roomWth);

printf("Customer discount (percent)?  ");
scanf("%d", custDsc);

printf("Cost per square foot (xxx.xx)?  ");
scanf("%f", costSqf);

printf("%6.2f is correct\n", *costSqf);
return;
}

/*calc*/
/*
This function calls three subfunctions.
*/
void calculate (int roomLth,
int roomWth,
int custDsc,
float costSqf,
float *install,
float *subTotal,
float *total)
{

/*statements*/

int a = roomLth;
int b = roomWth;
float c = costSqf;
int d = custDsc;
float e;
float f;
float g;

printf("\n %f is costSqf", costSqf);
g = calcInstall (a, b, c);
printf("\n %6.2f test\n", g);
e = calcSubTotal (g, d);
printf("\n%6.2f test subT \n", e);
/*      calcTotal (e, &f);

*install = g;
*subTotal = e;
*total = f;
*/
return;
}

/*calcI*/

/*
This calculates the installed price and cost.
*/

float calcInstall (int x, int y, float z)
{
int a;
float b;
float c;
float result;
printf("\n made it to calcI");
a = x * y;
b = z * a;
c = a * lab_Cost;
/*result = b + c;
*/
return (b + c);
}

/*calcS*/

float calcSubTotal (float inst, int dis)
{
float mult;
int hun = 100;
float ans;
printf("\n %d is the dis", dis);
printf("\n%6.2f is install ", inst);
mult = inst * dis;
ans = mult;

return ans;
}

/*calcT*/

void calcTotal (float e, float *f)
{

float a;

a = e * taxRate;
*f = a;
return;
}```

4. sorry, will do next time.

5. I'm sorry but this works with my compiler, the right value is printed. I'm using borland c++ builder 5.

6. i still can't get it to work for me
any suggestions?

7. Do you receive any warnings when u compile?
If so, post them, all of them.

8. this is the error i get when i am compiling my code as one file.

Code:
```silly.c:41: warning: type mismatch with previous implicit declaration
silly.c:25: warning: previous implicit declaration of `getData'
silly.c:41: warning: `getData' was previously implicitly declared to return `int'
silly.c:71: warning: type mismatch with previous implicit declaration
silly.c:27: warning: previous implicit declaration of `calculate'
silly.c:71: warning: `calculate' was previously implicitly declared to return `int'
silly.c:108: warning: type mismatch with previous implicit declaration
silly.c:85: warning: previous implicit declaration of `calcInstall'
silly.c:108: warning: `calcInstall' was previously implicitly declared to return `int'
silly.c:128: warning: type mismatch with previous implicit declaration
silly.c:87: warning: previous implicit declaration of `calcSubTotal'
silly.c:128: warning: `calcSubTotal' was previously implicitly declared to return `int'
515  \$```
thanks

by the way, that is trying to compile through gcc

9. The only advice i can give is :

1. Check that all the necessary header files are included in all your files.
2. Try using this program structure :

//Pre-Processor Directives / Header Files
...
//Funtion Prototypes
...
//main()
...
//Function Definitions
...

3. Check that the return types of your prototypes are the same as the definitions.

4. Pray for an answer.

This is 'bout the only advice i can give.

10. i just dont under stand how i can store 3 ints and a float in main using one function, but i can send the 3 int's but not the float to another function?

it doesn't make sense to me how such an easy task won't work?

i guess my question is how do i send

3ints and a float from main to another function?

11. If you want to change the value of the variables, then

Code:
```function(int*, int*, int*, float*);
main()
{
function(&int, &int, &int, &float);
...
...
}```
if you don't want their values changed, then just take the '*' and the '&' out.

12. thank you for all your help

Code:
```#include "chad.h"
int main (void)
{
int roomLth;
int roomWth;
int custDsc;
float costSqf;

/*statements*/

getData(&roomLth, &roomWth, &custDsc, &costSqf);

printf("%d = Lth %d = Wth %d = custDsc %7.2f = Sqf\n",  roomLth, roomWth, custDsc,
costSqf);

calculate(roomLth, roomWth, custDsc, costSqf);

return 0;
}```
this prints the right costSqf in main
but when i send it to calculate
the costSqf is not printed correctly.
if 22.23 is print in main as costSqf
then when i print from calculate
2.00 is printed?
weird?

13. > silly.c:71: warning: `calculate' was previously implicitly declared to return `int'
> silly.c:108: warning: type mismatch with previous implicit declaration
If you're still getting these warnings, then you HAVE to prototype your functions before you call them.

If there is no prototype, then C makes a GUESS, and in this case, it's guessing wrong.

Put prototypes for all your functions in chad.h

Popular pages Recent additions