# Thread: Passing variables to a subroutine

1. ## Passing variables to a subroutine

Hi everyone,

I'm trying to write a program that involves passing out some variables to a sub-routine to calculate some other variables. Here is the code:

Code:
```#include <stdio.h>
#include <math.h>

int main (int argc, const char * argv[]) {

double T;
double r[7][4], k[7][4], A[7] = { 1, 2, 3, 4, 5, 6, 7}, Ea[7] = { 10, 20, 30, 40, 50, 60, 70}, K[2][4], Ad[2], Ead[2];

void rateConstants(double k[7][4], double A[7], double Ea[7], double T);

T = 443.15;

rateConstants(k, A, Ea, T);

printf("\n %lf \n", k[1]);

return 0;
}

void rateConstants(double k[7][4], double A[7], double Ea[7], double T)
{
int i, j;

j = 0;

for (i=0; i<6; i++)
{
k[i][j] = A[i]*exp(-Ea[i]/(8.31*T));

printf("\n %lf", k[i][j]);

}

}```
As you can see, the subroutine prints out each value of k[i][j] that it calculates, then I have the main routine print out one of the k values to check if it's stored.However, the readout I actually get is:

0.997288
1.989167
2.975660
3.956787
4.932572
5.903034
-1.997117
logout

It doesn't matter what k value I choose, they all come out as -1.997117. Can anyone see what I'm doing wrong? I wrote a similar program for a much simpler case (as a test), and it all seemed to work.

I should warn you that I am not a brilliant programmer, so you may have to explain things to me quite explicitly.

2. Code:
`	printf("\n %lf \n", k[1]);`
k[1] is not a double value

should be
Code:
`	printf("\n %lf \n", k[1][0]);`
or
Code:
`	printf("\n %lf \n", *k[1]);`
if you wish

Kurt

3. Thank you very much! It's the first option that I want - I really should have noticed that.

Also, is there an easier way to operate on/print out an array than the loop I have in the subroutine?

Sam

4. Originally Posted by shaig
is there an easier way to operate on/print out an array than the loop I have in the subroutine?
I'd say that any operation on an array involves some kind of loop.
Kurt

5. OK, thanks for the help!

Sam

6. When printing out doubles with printf(), %f should be used, not %lf. In the older (and generally currently implemented) C standard, %lf in printf() is not defined. While a lot of C libraries support %lf to mean %f, there's no guarantee, and it's certainly possible that you might run across an implementation that doesn't know what to do with %lf.

%lf should still be used for doubles with scanf(), though.