li******@gmail.com wrote:
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
It depends on how 'Pass by Reference' is interpreted.
In general computer science, passing a pointer to some data qualifies
as 'Pass by Reference'.
In the context of C++, and for those who want to distinguish between the
parameter passing methods of C and C++, the term 'Pass by Reference'
gets reserved for the situation that C++ reference types are used. The
mechanism of passing a pointer is then referred to as 'Pass by
Address'.
You could also argue that C only has 'Pass by Value', because if you are
passing a pointer to something, then it is the value of the pointer
that gets passed.
>
Also I read that, "C does not have run-time typing". What does this
mean and is it true?
You will have to ask whoever made that statement what they meant with
the term 'run-time typing'.
One main characteristic of C is that all types used in the program, as
well as the exact type for each variable, must be known to the
compiler.
>
C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?
Yes, that is correct.
>
I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:
/* Example 1 */
/* func.c */
int swap(int a, int b)
{
a = b;
return a;
}
Given this implementation, I think the name 'assign' would have been
better for this function (same goes for the other examples).
This function uses 'Pass by Value', and therefor any changes you make to
a and b will be discarded as soon as you leave the function.
>
/* main.c */
int b =1;
int c = 2;
int main(void)
{
swap(b,c);
printf("%d",c);
return 0;
}
BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local. In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?
The default should be to use local variables.
The advantage of local variables is that they are only visible within
the block where they are declared. This means that in different
(non-nested) blocks, you can re-use the same name for different
purposes.
Global variables should be avoided as much as possible. Use them only
when there is no reasonable alternative and even then you should prefer
to declare them as 'static', which will limit their visibility to the
current source file.
>
/* Example 2 */
/* func2.c */
int swap(int &a, int &b)
This is a syntax error in C.
<snip>
/* Example 3 */
/* func3.c */
int swap(int *a, int *b)
{
*a = *b;
return *a;
}
This function uses 'Pass by Address'.
Changes to a and b themselves will not be visible or preserved outside
the function, but changes to *a or *b are visible and preserved.
>
/* main3.c */
int *b =1;
int *c = 2;
These two lines should generate a loud complaint from the compiler.
For this example, it is best to declare b and c the same as in example 1
int b =1;
int c = 2;
>
int main(void)
{
swap(*b,*c);
This is the wrong syntax for calling the function.
You need to pass the address of b and c, so you call the function like
this:
swap(&b, &c);
printf("%d",&c);
This should just be:
printf("%d", c);
But note that the function swap() does not actually modify its second
argument, so you will see no change here.
return 0;
}
A more useful illustration of 'Pass by Value and 'Pass by Address' would
be this:
#include <stdio.h>
void swap_value(int lhs, int rhs)
{
int temp;
printf("Inside swap_value(in): lhs = %d, rhs = %d\n", lhs, rhs);
temp = lhs;
lhs = rhs;
rhs = temp;
printf("Inside swap_value(out): lhs = %d, rhs = %d\n", lhs, rhs);
}
void swap_address(int* lhs, int* rhs)
{
int temp;
printf("Inside swap_address(in): *lhs = %d, *rhs = %d\n", *lhs, *rhs);
temp = *lhs;
*lhs = *rhs;
*rhs = temp;
printf("Inside swap_address(out): *lhs = %d, *rhs = %d\n", *lhs,
*rhs);
}
int main()
{
int a;
int b;
a = 1;
b = 42;
printf("Before swap_value(a,b): a = %d, b = %d\n", a, b);
swap_value(a,b);
printf("After swap_value(a,b): a = %d, b = %d\n", a, b);
a = 1;
b = 42;
printf("Before swap_address(a,b): a = %d, b = %d\n", a, b);
swap_address(a,b);
printf("After swap_address(a,b): a = %d, b = %d\n", a, b);
return 0;
}
>
PS: I'm learning about closures in Lisp and they seem nifty, but I
read C doesn't have them. Or is this not quite true?
C does indeed not have closures.
>
Lisp 9000
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ:
http://www.comeaucomputing.com/learn/faq
c.l.c FAQ:
http://c-faq.com/
c.l.c++ FAQ:
http://www.parashift.com/c++-faq-lite/