Forums

Passing variables between functions

Started by smo59 December 7, 2004
I am programming a TI DM642 in C.

When passing data between two functions, it seems to me that it would
be more efficient to pass a pointer rather than a copy of the variable.

Example.
This ....
void main() {
unsigned char num = 2;
unsigned char *ptr = #
double(ptr);
}

void double(unsigned char *ptr) {
*ptr = *ptr * 2;
}

... is better than this ....

void main() {
unsigned char num;
num = double(num);
}

unsigned char double(unsigned char num) {
num = num * 2;
return num;
}

Any thoughts, or have I over-simplified something quite basic?
(My brain has stopped because of a cold, so please be gentle.)
Cheers.
smo59.

Wouldn't a good compiler do this automatically?

smo59 wrote:
> I am programming a TI DM642 in C. > > When passing data between two functions, it seems to me that it would > be more efficient to pass a pointer rather than a copy of the variable. > > Example. > This .... > void main() { > unsigned char num = 2; > unsigned char *ptr = # > double(ptr); > } > > void double(unsigned char *ptr) { > *ptr = *ptr * 2; > } > > ... is better than this .... > > void main() { > unsigned char num; > num = double(num); > } > > unsigned char double(unsigned char num) { > num = num * 2; > return num; > } > > Any thoughts, or have I over-simplified something quite basic? > (My brain has stopped because of a cold, so please be gentle.) > Cheers. > smo59. >
-- Please change no_spam to a.lodwig when replying via email!
Well... it really depends on the size of the data you want to pass. A 
pointer itself really requires more than the space for a char (depends 
upon the address bus size of the processor). So if the variable you want 
to pass is an array for example, it would make sense.

Also, usually, a pre-defined number of internal registers are allocated 
for passing arguments (in the ARM, the compiler uses r0 - r4 to pass 
arguments. I do not know how the TM642 compiler handles it). The 
compiler then could directly use the values in the registers if they 
were passed as values instead of performing an extra memory access. So 
depending on the number of parameters passing values could be a lot more 
efficient than passing pointers.

- Ravi
smo59 wrote:

> I am programming a TI DM642 in C. > > When passing data between two functions, it seems to me that it would > be more efficient to pass a pointer rather than a copy of the variable. > > Example. > This .... > void main() { > unsigned char num = 2; > unsigned char *ptr = # > double(ptr); > } > > void double(unsigned char *ptr) { > *ptr = *ptr * 2; > } > > ... is better than this .... > > void main() { > unsigned char num; > num = double(num); > } > > unsigned char double(unsigned char num) { > num = num * 2; > return num; > } > > Any thoughts, or have I over-simplified something quite basic? > (My brain has stopped because of a cold, so please be gentle.) > Cheers. > smo59.
There's more than a semantic difference between "pass by value" and "pass by reference". The results of the program differ in many cases. One can think of these subtle differences as side effects, but there are no side affects in reality. The computer does what you (or the @#$%& compiler behind your back) tell it to. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Jerry Avins wrote:

  ...

> There's more than a semantic difference between "pass by value" and > "pass by reference". ...
See http://www.thunderstone.com/texis/site/tutorial/vxfuncref.html Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
] void main() {
] unsigned char num = 2;
] unsigned char *ptr = #
] double(ptr);
^^^^^^^^

Just out of interest, did this compile?

Ciao,

Peter K.

Peter K. wrote:
> ] void main() { > ] unsigned char num = 2; > ] unsigned char *ptr = # > ] double(ptr); > ^^^^^^^^ > > Just out of interest, did this compile? > > Ciao, > > Peter K.
I didn't try to compile it. I just wrote it straight into this post to demonstrate the intention of the code rather than worry about what the actual code would be. (That is not to say I didn't think about the code and how I would write it were I intending to compile it.)
OK.  I am just generally averse to using keywords as function names.
:-)

Ciao,

Peter K.

Peter K. wrote:
> OK. I am just generally averse to using keywords as function names. > :-) > > Ciao, > > Peter K.
So am I usually. It's a fair cop.